Android GCM:显示消息
当我通过GCM发送消息时,它会毫无问题地出现在通知栏上。如果我单击此通知以在活动中显示它;它不显示消息 但是如果我开始我的活动(NotificationDisplay),然后发送消息,我的消息就会显示出来 因此,如何在notstarted活动中显示通知消息 (我认为问题可能是“registerReceiver”:它在NotificationDisplay onCrate中;所以如果活动未启动,则无法注册?) GCMinentService:Android GCM:显示消息,android,google-cloud-messaging,android-notifications,Android,Google Cloud Messaging,Android Notifications,当我通过GCM发送消息时,它会毫无问题地出现在通知栏上。如果我单击此通知以在活动中显示它;它不显示消息 但是如果我开始我的活动(NotificationDisplay),然后发送消息,我的消息就会显示出来 因此,如何在notstarted活动中显示通知消息 (我认为问题可能是“registerReceiver”:它在NotificationDisplay onCrate中;所以如果活动未启动,则无法注册?) GCMinentService: @Override protected voi
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("msg");
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
context.sendBroadcast(intent);
generateNotification(context, message);
}
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, NotificationPage.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
通知显示活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_page);
lblMessage = (TextView) findViewById(R.id.lblMessage);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
}
public final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
lblMessage.setText(newMessage + "\n");
// lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
检查是否调用onCreate。如果每次单击通知区域的通知时都会调用onCreate,则替换代码:
`PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0)`
与
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
如果未调用onCreate,请尝试更新onResume或onNewIntent方法上的文本。如果您的问题得到解决,请告诉我我的问题与原始海报相同。如何确定是否调用了onCreate?我确实在我的GCMinentService中添加了“PendingEvent.FLAG_UPDATE_CURRENT”。此外,我不知道如何更新onResume方法以接收那里的意图。请尝试登录onCreate/onResume/onNewIntent,并让我知道调用的是哪一个?学习如何创建日志,在我弄清楚后将报告。使用Util包中的log类。。。。。好的,日志也是这样,当我按下通知栏消息时,它确实调用了onCreate。也许我应该开始一个新的问题,这样我也可以发布我的代码。