设备上未收到Android GCM消息

jampez77

我试图设置我的应用程序以接收推送通知。但是,问题在于,尽管它们是从服务器成功发送的,但它们并未到达设备。

这是我的代码:

显现

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission android:name="com.jampez.smalltalk.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="com.jampez.smalltalk.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<activity
        android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

      <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

            <category android:name="com.jampez.smalltalk" />
        </intent-filter>
    </receiver>

GCMIntentService.java

在其他活动的主要内容中

public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    displayMessage(context, "Your device registred with GCM");
    ServerUtilities.register(context, registrationId);
}

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
    displayMessage(context, getString(R.string.gcm_unregistered));
    ServerUtilities.unregister(context, registrationId);
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("message");

    displayMessage(context, message);
    // notifies user
    generateNotification(context, null, message, (long) 01);
}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    String message = getString(R.string.gcm_deleted, total);
    displayMessage(context, message);
    // notifies user
    generateNotification(context, null, message, (long) 00);
}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
    displayMessage(context, getString(R.string.gcm_error, errorId));
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    displayMessage(context, getString(R.string.gcm_recoverable_error,
            errorId));
    return super.onRecoverableError(context, errorId);
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */
private static void generateNotification(Context context, String title, String message, Long extra) {
    int icon = R.drawable.ic_launcher;

    Integer i = (int) (long) extra;

    if(title == null){
     title = context.getString(R.string.app_name);
    }

    Intent notificationIntent  = new Intent(context, MainActivity.class);

    PendingIntent Int = PendingIntent.getActivity(context, i, notificationIntent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(icon)
            .setContentTitle(title)
            .setContentIntent(Int)
            .setContentText(message)
            .setAutoCancel(true) 
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(i, mBuilder.build());


}
}

AppConfig.java

public static final String DISPLAY_MESSAGE_ACTION = "com.jampez.smalltalk.DISPLAY_MESSAGE";

public static final String TAG = "GCM";

static final String EXTRA_MESSAGE = "message";

/**
 * Notifies UI to display a message.
 * <p>
 * This method is defined in the common helper because it's used both by
 * the UI and the background service.
 *
 * @param context application's context.
 * @param message message to be displayed.
 */
public static void displayMessage(Context context, String message) {
    Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
    intent.putExtra(EXTRA_MESSAGE, message);
    context.sendBroadcast(intent);
}

ServerUtilities.java

public final class ServerUtilities {
private static final int MAX_ATTEMPTS = 5;

/**
 * Register this account/device pair within the server.
 *
 */
public static void register(final Context context, final String regId) {
    Log.i(TAG, "registering device (regId = " + regId + ")");


    // Once GCM returns a registration id, we need to register on our server
    // As the server might be down, we will retry it a couple
    // times.
    for (int i = 1; i <= MAX_ATTEMPTS;) {
        Log.d(TAG, "Attempt #" + i + " to register");
        displayMessage(context, context.getString(R.string.server_registering, i, MAX_ATTEMPTS));
        GCMRegistrar.setRegisteredOnServer(context, true);
        String message = context.getString(R.string.server_registered);
        AppConfig.displayMessage(context, message);
        return;
    }
    String message = context.getString(R.string.server_register_error,
            MAX_ATTEMPTS);
    AppConfig.displayMessage(context, message);
}

/**
 * Unregister this account/device pair within the server.
 */
public static void unregister(final Context context, final String regId) {
    Log.i(TAG, "unregistering device (regId = " + regId + ")");
    Map<String, String> params = new HashMap<String, String>();
    params.put("regId", regId);
    //post(serverUrl, params);
    GCMRegistrar.setRegisteredOnServer(context, false);
    String message = context.getString(R.string.server_unregistered);
    AppConfig.displayMessage(context, message);
}

}
阿奇特·戈尔(Archit Goel)

我不知道这是否是主要原因,但是您缺少权限。由于GCM需要Google帐户,因此您也应该声明以下权限:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

另外,也声明一个service

<service android:name=".GCMIntentService" />

编辑:

如果错误仍然存​​在,请尝试以下操作:

转到项目/属性/ Java Build Path / Order and Export-确保在使用Android Dependencies和支持库之前已进行检查。标记所有复选框。单击“应用”并清理项目。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章