【问题标题】:How to change message content of PushNotification?如何更改 PushNotification 的消息内容?
【发布时间】:2014-03-04 19:19:57
【问题描述】:

我在我的 Android 设备上收到通知。我的 cotification 消息内容显示:

我正在使用 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();
}

我怎样才能将通知的消息内容从 更改为与 gcm 通知一起从服务器发送给我的 JSON 字符串格式?
我想要的类型是:

请在这种情况下帮助我。
任何帮助将不胜感激...
:)

【问题讨论】:

  • 您用于从服务器发送消息的代码在哪里?
  • 其实,我在客户端开发团队。我的服务器端团队告诉我,他们正在发送上述给定格式的 json 字符串,我必须在消息内容中显示。

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


【解决方案1】:

我自己不使用 Phonegap,但我只能假设这些 JSON 元素作为附加组件进入 GCM,就像“消息”一样。

因此在你的 Java 代码中有:

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

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

mBuilder.setContentText(message);

.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2014-11-24
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多