gpt4 book ai didi

android - 如何更改 PushNotification 的消息内容?

转载 作者:行者123 更新时间:2023-11-29 21:13:05 24 4
gpt4 key购买 nike

我在我的 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 字符串格式?
我想要那种类型的:
<任务名称、任务类型、任务描述>
请在这方面帮助我。
任何帮助将不胜感激...
:)

最佳答案

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

因此在你的 Java 代码中有:

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

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

mBuilder.setContentText(message);

.

关于android - 如何更改 PushNotification 的消息内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com