0

我正在尝试使用GCM开发推送通知服务。我能够接收从服务器发送到我的设备的通知。点击通知后,应用程序将打开。好。现在我想要的是通知上的消息也应该显示在应用程序中。我怎样才能做到这一点?从通知中获取消息并在应用上显示

Code in GCMintentservice.java: 
private void sendNotification(String msg) { 
     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.ic_action_search_) 
     .setContentTitle("GCM Notification") 
     .setStyle(new NotificationCompat.BigTextStyle() 
     .bigText(msg)) 
     .setContentText(msg); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 

现在我得到的通知,但点击通知我去应用程序,但消息不存在。我该怎么办。我在主应用程序中有一个文本视图。我如何使用来自通知的消息更新文本视图?主应用中的OnCreate可能需要做些什么?请帮忙。

问候

回答

2

应提供包含GCM的细节到的PendingIntent的包裹意图软件包:

Intent intent = new Intent(this, MainActivity.class) 
intent.putExtra(...); 
PendingIntent contentIntent = PendingIntent.getActivity(
     this, 0, 
     intent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 
+0

我应该在哪里把这个上面的代码? – Saty

+0

我应该如何在MainActivity类中获取值? – Saty

+0

@Saty看来你缺少一些基础知识。我建议读一下[内容是什么](http://developer.android.com/guide/components/intents-filters.html),然后跟随[关于通知的此信息](http:// developer .android.com /引导/主题/ UI /通知器/ notifications.html)。 –

相关问题