如何更改PushNotification的消息内容?

问题描述:

我收到我的android设备上的通知。 cotification我的邮件内容显示:
<丢失邮件内容>
我使用PushPlugin(https://github.com/phonegap-build/PushPlugin)项目,以建立我的代码。我NotificationController.js代码
部分是:
如何更改PushNotification的消息内容?

registerDevice: function() {  
    var pushNotification = window.plugins.pushNotification; 

    if (device.platform == 'android' || device.platform == 'Android') { 
     pushNotification.register(AppDemo.app.getControllerInstances()['AppDemo.controller.NotificationController'].recieveGCMRegistrationId, 
       AppDemo.app.getControllerInstances()['AppDemo.controller.NotificationController'].errorHandler, 
       {"senderID":"624986650855","ecb":"AppDemo.app.getControllerInstances()['AppDemo.controller.NotificationController'].onNotificationGCM"}); 
    } 
}, 
recieveGCMRegistrationId: function(result) { 
    AppDemo.app.getControllerInstances()['AppDemo.controller.NotificationController'].handleRegistrationId(result, 'GCM'); 
}, 
onNotificationGCM: function(event) 
{ 

    switch(event.event) { 
     case 'registered': 
     alert("regd id"+event.regid); 
      this.handleRegistrationId(event.regid, 'GCM'); 
      break; 

     case 'message': 
      alert('Notification Received'); 
      break; 

     case 'error': 
      alert('Error received from GCM Server : ' + error); 
      break; 

     default: 
      break; 
    } 
}, 
handleRegistrationId: function (registrationId, deviceType) 
{ 
    window.localStorage.setItem("registrationId", registrationId); 
} 

我的服务器是在数据的发送JSON字符串非常久远的GCM通知:

{"taskName”:" test task name","taskType":" Adhoc Task","taskDescription":"test task description"} 

代码中GCMIntentService.java在我的Android编译项目是:

public void createNotification(Context context, Bundle extras) 
{ 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    String appName = getAppName(this); 

    Intent notificationIntent = new Intent(this, PushHandlerActivity.class); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    notificationIntent.putExtra("pushBundle", extras); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);   

    NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(context) 
      .setSmallIcon(context.getApplicationInfo().icon) 
      .setWhen(System.currentTimeMillis()) 
      .setContentTitle(appName) 
      .setTicker(appName) 
      .setContentIntent(contentIntent); 

    String message = extras.getString("message"); 
    if (message != null) { 
     mBuilder.setContentText(message); 
    } else { 
     mBuilder.setContentText("<missing message content>"); 
    } 

    String msgcnt = extras.getString("msgcnt"); 
    if (msgcnt != null) { 
     mBuilder.setNumber(Integer.parseInt(msgcnt)); 
    } 

    mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build()); 
    tryPlayRingtone(); 
} 

我怎么能够改变我的通知邮件内容来自<缺少邮件内容>以JSON字符串格式从服务器发送给我以及gcm通知?
我想要的类型:
< TASKNAME,任务类型,taskDescription >
请帮我在这方面。
任何帮助将非常感激...
:)

+0

您用来从服务器发送消息的代码在哪里? – Eran

+0

其实,我在客户端开发团队。我的服务器端团队告诉我他们正在发送上述给定格式的json字符串,并且我必须在消息内容中显示。 – DMajumdar

我不使用PhoneGap的自己,但我只能假设,这些JSON中的元素进入GCM当临时演员,就像“消息”。

在Java代码中

因此有:

String taskName = extras.getString("taskName"); 
String taskType = extras.getString("taskType"); 
String taskDescription = extras.getString("taskDescription"); 

做一个空检查他们,然后串联/将这些字符串格式化到您发送到mBuilder“消息”。

mBuilder.setContentText(message); 

+0

它工作。谢谢你的帮助... :) – DMajumdar