我想显示我的C2DM消息的有效负载中包含的消息,但我似乎无法在不崩溃的情况下使其工作。
package com.themenetwork.app;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class C2DMMessageReceiver extends BroadcastReceiver {
public NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("c2dm", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("c2dm", "Received message");
final String payload = intent.getStringExtra("payload");
Log.d("c2dm", "dmControl: payload = " + payload);
Notification notification = new Notification(R.drawable.icon, "Hello", 0);
Intent notificationIntent = new Intent(context, RootActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, "Title", "Text", contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
myNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
}
例外是......
线程[< 1> main](Suspended(例外RuntimeException))
ActivityThread.handleReceiver(ActivityThread $ ReceiverData)行: 1881 ActivityThread.access $ 2400(ActivityThread, ActivityThread $ ReceiverData)line:124
ActivityThread $ H.handleMessage(消息)行:1018
ActivityThread $ H(Handler).dispatchMessage(Message)行:99 Looper.loop()行:130 ActivityThread.main(String [])行:3806
Method.invokeNative(Object,Object [],Class,Class [],Class,int, boolean)line:not available [native method] Method.invoke(Object, 对象...)行:507 ZygoteInit $ MethodAndArgsCaller.run()行:839
ZygoteInit.main(String [])行:597 NativeStart.main(String []) line:not available [native method]
我是Java的新手,更不用说Android了,所以我完全不知道为什么这不起作用。
答案 0 :(得分:0)
当您尝试在其上调用myNotificationManager
时,notify
是否为空?您需要初始化它以指向NotificationManager
的有效实例,可能是这样的:
myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);