Nor*_*ora 20 android google-cloud-messaging
我的Android应用程序崩溃,这是logcat: -
java.lang.NullPointerException
at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:194)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
Run Code Online (Sandbox Code Playgroud)
我查看了android gcm r3源码,发现onHandleIntent()中的参数intent为null.
这甚至可能吗?怎么解决?
(我知道Service.onStartCopmmand返回时可以看到null意图,START_STICKY
但IntentService.onStartCommand不会使用START_STICKY.)
小智 1
我认为应用程序仅在安装期间在某些设备上崩溃。这是因为在安装过程中,GCM 服务还会Intent从其他 Google 源接收一些内容,而您的广播接收器不准备处理此类类型的Intent.
如果您只想接收通过推送通知从服务器拉取的 GCM Intent,则只需在处理 Intent 调用中使用它即可。
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
//String msg = intent.getStringExtra("message");
String from=extras.getString("from");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendErrorNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendErrorNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++) {
Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
// sendNotification("Received: " + extras.toString());
/*********ERROR IN SOME DEVICES*****************/
if(from.equals("google.com/iid"))
{
//related to google ... DO NOT PERFORM ANY ACTION
}
else {
//HANDLE THE RECEIVED NOTIFICATION
String msg = intent.getStringExtra("message");
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
/**************************/
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2443 次 |
| 最近记录: |