1

我正在使用node-gcm向 android 设备发送通知,在某些情况下,我需要发送带有通知的图像以显示为缩略图,如下所示: notification with thumbnail

有时我需要发送没有缩略图的通知。使用下面的代码,我可以在通知中发送图像,问题是当收到另一个通知时,它们会崩溃,使新通知覆盖已经存在的通知:

    var message = new gcm.Message({
                "data" : {
                    "title" : "Test",
                    "message" : "Test message!",
                    "priority" : 2, // Highest priority.
                    "ledColor" : [255, 0, 0, 1],
                    "content-available": "1",
                    "image": req.body.notificationImageUrl, //<-- image URL
                },
            });

但是,如果我像下面这样设置消息,我找不到发送图像的方法,但通知不会折叠并且所有通知都会出现。另一个问题是这个版本的led没有激活:

    var message = new gcm.Message({
                data: {
                    "priority" : 2, // Highest priority.
                    "content-available": "1",
                    "image": req.body.notificationImageUrl,  // <-- image URL
                },
                notification:{
                    title: "Test",
                    body: "Test message!",
                    color: "#892121",
                    sound: 'default',
                    vibrationPattern: [300, 150, 300], // Vibrate for 300ms then wait 150ms and then vibrate for 300ms.
                    ledColor: [255, 0, 0, 1], // <-- this does not work
                },
            });

我想要的是一种发送带有缩略图的通知的方法,它不会覆盖以前存在的通知。

编辑:我忘了提一下,我将 Ionic Framework 与 Cordova 一起使用,所以我无法管理设备中的通知,除非有办法通过这些框架来做到这一点。提前致谢!

4

2 回答 2

2

在处理node-gcm 问题时,我找到了解决方案,有一个名为“notId”的参数可以将 ID 添加到通知中。

 var message = new gcm.Message({
     "data": {
         "title": "Test",
         "message": "Test message!",
         "priority": 2, // Highest priority.
         "ledColor": [255, 0, 0, 1],
         "content-available": "1",
         "image": req.body.notificationImageUrl,
         "notId": parseInt(Math.random() * new Date().getSeconds(), 10), // <-- this solved the problem
     },
 });
于 2017-03-22T18:31:11.790 回答
0

通知显示的管理在设备上完成。您必须确保在 Android 上为每个通知设置不同的 NOTIFICATION_ID,否则最后一个将始终替换它之前的那个。

// Sets an ID for the notification - make sure to change this for different notifications
int mNotificationId = 001;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Builds the notification and issues it
mNotifyMgr.notify(mNotificationId, mBuilder.build());
于 2017-03-22T18:02:58.020 回答