0

我已经在我的应用程序中实现了android通知,它工作正常,除了它显示一个数字而不是实际的消息体。下面是我得到的德截屏,获取数字而不是文本的android通知

enter image description here

这是代码我有,

public static final int MESSAGE_NOTIFICATION_ID = 435345; 
    private int MESSAGE_TYPE ; 


    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     String type = data.getString("type"); 
     if(type.equalsIgnoreCase("Load Messages")) 
     { 
      MESSAGE_TYPE = Global.NOTIFICATION_LOAD_MESSAGE; 
      EventBus.getDefault().post(new HandyManEvents.ReloadMessages(true)); 
     } 
     else 
     { 
      MESSAGE_TYPE = Global.NOTIFICATION_LOAD_LIVE_JOBS; 
     } 
     createNotification(from, message); 
    } 

    // Creates notification based on title and body received 
    private void createNotification(String title, String body) { 

     Context context = getBaseContext(); 

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     notificationIntent.putExtra("menuFragment", MESSAGE_TYPE); 
     PendingIntent pending= PendingIntent.getActivity(context, 0,notificationIntent, 0); 


     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title) 
       .setContentIntent(pending) 
       .setContentText(body); 
     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build()); 

    } 

任何线索怎么回事错在这里?

更新 当应用程序在前台运行时,我看到了这种行为。这是我从通知中得到的包,

Bundle[{type=Load Messages, notification=Bundle[{e=1, body=You have a new message, icon=app_icon, title=New Message}], collapse_key=com.company.app}] 

如何从Bundle中提取标题和正文?

谢谢。

+0

推测“data.getString(”message“)”是该值。 '捆绑数据'来自哪里? – CommonsWare

+0

捆绑数据来自服务器。这主要发生在我接收到应用程序打开的通知时。 – Zach

回答

0

您正在使用谷歌播放服务API来捕获GCM消息。此代码所属的类是GcmListenerService的扩展类,并且您将覆盖onMessageReceived(String from, Bundle data),它将发件人ID作为第一个参数(来自),并将您的通知指定为标题时出现在通知中。

您需要以正确的方式解析数据包以获取数据,这取决于服务器发送的有效负载。您可以通过登录查看捆绑包中可用的密钥

for (String key : bundle.keySet()){ 
    Log.d(TAG, key + " = " + bundle.get(key)); 
} 
+0

感谢您的回复。你是对的,但只有当我在前台使用应用时才会发生这种情况。如果它在后台我正在收到适当的通知。请参阅最新的问题。 – Zach

+0

您不应该修改问题以提供另一个问题。 –

相关问题