使用 node-gcm 发送 android 通知而不折叠它们

Send android notification with node-gcm without collapsing them

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

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

编辑: 我忘了说,我在 Cordova 中使用 Ionic Framework,所以我无法管理设备中的通知,除非有办法通过这些框架来完成. 提前致谢!

通知显示的管理在设备上完成。您必须确保在 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());

通过 node-gcm issues 我找到了解决方案,有一个名为 "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
     },
 });