如何更改推送通知的消息内容?

时间:2014-03-04 19:19:58

标签: android cordova push-notification sencha-touch google-cloud-messaging

我在Android设备上收到通知。我的消息内容显示: <缺少邮件内容>
我正在使用PushPlugin(https://github.com/phonegap-build/PushPlugin)项目来构建我的代码 我的NotificationController.js代码的一部分是:

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"}

我的Android构建项目中的GCMIntentService.java内的代码是:

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();
}

我怎样才能将通知的消息内容从&lt;缺少消息内容&gt; 更改为从服务器发送给我的JSON字符串格式以及gcm通知?
我想要那种类型:
&lt; taskName,taskType,taskDescription&gt;
请在这方面帮助我。
任何帮助都会非常感激...
:)

1 个答案:

答案 0 :(得分:2)

我自己不使用Phonegap,但我只能假设这些JSON元素作为额外内容进入GCM,就像'message'一样。

因此在您的Java代码中有:

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

对它们进行空检查,然后将这些字符串连接/格式化为发送给mBuilder的“消息”。

mBuilder.setContentText(message);