I have something like this in a Node app:
var sender = new gcm.Sender("XPTO");
var registrationIds = ["whatever"];
...
var message = new gcm.Message({
data: {
avatar: body_data.avatar,
message: body_data.message
}
});
sender.send(message, registrationIds, 4, function (err, result) { console.log("success"); });
It works fine, the notification arrives and goes to the tray if the app if not opened.
But if I send a new notification to the same registrationId, the old notification is "updated" (or removed) and only the new one is shows.
If I add a random integer as parameter to notId
message.addData("notId", parseInt(Math.random() * 25));
the notifications are kept in the tray, but then the tray start to show multiple notifications.
Is there a way to group the notifications?
Android devices group same kind of notifications. If you set set different collapseKey for each kind of notification, they won't get grouped with others. You can have at most 4 different collapseKey at the same time visible to user in tray.
Related
I have a Notification Hub in Azure to send PUSH to Android and iOS devices. Everything is working well in Android. In iOS I have this problem:
I turn off the WiFi or I turn off the device
I send chat messages to my app (push notifications) and also messages to that device from WhatsApp.
I turn on the WiFi or turn on the device after a few minutes
I receive all the WhatsApp messages notifications but no one of the notification to my App. If the device and Internet is on, I receive the notifications without problem.
My question is: Anyone has experienced something like that or know how to fix it?
At least I should have to receive the last push, right?
I'm sending the push to a tag. I can see the Device registered in the hub either when WiFi or iPhone are off.
When sending notification ensure that you set the expiration. Default value is zero. Hence, APNs treats the notification as if it expires immediately and does not store the notification or attempt to redeliver it.
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html
apns-expiration
If this value is nonzero, APNs stores the notification
and tries to deliver it at least once, repeating the attempt as needed
if it is unable to deliver the notification the first time. If the
value is 0, APNs treats the notification as if it expires immediately
and does not store the notification or attempt to redeliver it.
If you are using template notifications, here is how you do it.
AppleTemplateRegistrationDescription registration = new AppleTemplateRegistrationDescription(parameters.registrationID)
{ BodyTemplate = new CDataMember("{\"aps\":{\"alert\":\"$(body)\",\"payload\":\"$(payload)\",\"deeplinking\":\"$(deeplinking)\",\"category\":\"$(category)\",\"image\":\"$(image)\"}}"),
Tags = itags,
Expiry = "$(APNS_Expiry)"
};
As part of send, you can pass the expiry value.
var notification = new TemplateNotification(new Dictionary<string, string>()
{
{"APNS_Expiry", DateTime.UtcNow.AddMinutes(10).ToString("o") }, //Timestamp
{"body", NotificationText},
{"payload", NotificationText},
{"deeplinking", payload},
});
var Responsehub = hub.SendNotificationAsync(notification);
If you are using native notifications,
// Native notification
var notification = new AppleNotification(#"
{
""aps"": {
""alert"":""New notification!""
}
}");
notification.Expiry = DateTime.UtcNow.AddMinutes(2);
await notificationHubClient.SendNotificationAsync(notification);
If you are using template notifications, here is how you do it.
AppleTemplateRegistrationDescription registration = new AppleTemplateRegistrationDescription(parameters.registrationID)
{ BodyTemplate = new CDataMember("{\"aps\":{\"alert\":\"$(body)\",\"payload\":\"$(payload)\",\"deeplinking\":\"$(deeplinking)\",\"category\":\"$(category)\",\"image\":\"$(image)\"}}"),
Tags = itags,
Expiry = "$(APNS_Expiry)"
};
As part of send, you can pass the expiry value.
var notification = new TemplateNotification(new Dictionary<string, string>()
{
{"APNS_Expiry", DateTime.UtcNow.AddMinutes(10).ToString("o") }, //Timestamp
{"body", NotificationText},
{"payload", NotificationText},
{"deeplinking", payload},
});
var Responsehub = hub.SendNotificationAsync(notification);
If you are using native notifications,
// Native notification
var notification = new AppleNotification(#"
{
""aps"": {
""alert"":""New notification!""
}
}");
notification.Expiry = DateTime.UtcNow.AddMinutes(2);
await notificationHubClient.SendNotificationAsync(notification);
I am trying to leverage Slack's Real-time messaging (RTM) API to post a welcome message to newly joined users of my team from a "Greeter Bot".
The event I'm listening for is team_join.
I have confirmed that the event is firing, and that messages can be sent to previously joined users out upon receiving that event. However, when trying to notify the newly joined user, nothing comes through.
I've played around with adding a delay (up to 45secs) after receiving the event before notifying the user, but still no dice.
Here my index.js file:
var SlackBot = require('slackbots');
var bot = new SlackBot({
token: process.env.SLACK_TOKEN || '',
name: process.env.BOT_NAME || 'greeterbot'
});
bot.on('message', function(data) {
var self = this;
if ('team_join' === data.type) {
setTimeout(function() {
var message = 'hello.';
// this works. 'some crusty old user' gets a DM message from my greeterbot.
self.postMessageToUser('some crusty old user', message, { as_user: true });
// FAIL. what am i doing wrong?!
self.postMessageToUser(data.user.name, message, { as_user: true });
}, 45000);
console.log("'" + data.user.name + "' has joined the team."); // works. the user name is present in the log.
}
});
I figured it out. There is a caching issue with the underlying bot library that I'm using.
For reference, here is a link to a PR that aims to address this issue.
https://github.com/mishk0/slack-bot-api/pull/25
Switching libs now.
Our Slack team, SKGTech.io uses Janitr that we wrote. Janitr is a Slack bot that welcomes new users based on your preferences.
I am currently using PubNub SDK for chatting in website and iPhone. Now i want to implement functionality like i want to send message from iPhone to web-end once user sends a message from iPhone to web-end ( user will be notified ).
Now the issue is when initially user1 sends message from iPhone to webend i am not getting notification.
on first message i am not getting notification.
code is somewhat like this
var socket = new WebSocket("Pubnub + channel1id";
var chat_socket = new WebSocket("Pubnub + channel2id";
socket.onmessage = function (evt)
{
//Here i implemeted our notification functionality.
}
chat_socket.onmessage = function (evt)
{
//Here i implemeted our notification functionality.
}
on second message i am getting object on .onmessage but not for first.
I have some problems with my project, i hope you will help me!
Description:
I use library from https://github.com/ToothlessGear/node-gcm to build server push notification for some devices.
I have configured API key from Google Cloud Message to SenderId.
My code:
var message2 = new gcm.Message();
**// ... or some given values
var message2 = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: 'message1',
key2: 'message2'
}
});
// Change the message data
// ... as key-value
message2.addData('key1','message1');
message2.addData('key2','message2');
// ... or as a data object (overwrites previous data object)
message2.addData({
key1: 'message1',
key2: 'message2'
});
// Change the message variables
message2.collapseKey = 'demo';
message2.delayWhileIdle = true;
message2.timeToLive = 3;
message2.dryRun = true;
// Set up the sender with you API key
var sender = new gcm.Sender('AIzaSyBzb1PsEBEiAjagslkEANqyxxxxxxxxx');
// Add the registration IDs of the devices you want to send to
var registrationIds = [];
registrationIds.push('APA91bHAbFJYpHsSN-uz1Hkh8XjBw4xU87VIz5hZYOtobVv6I2pUZM67pWUGbb5zcP2HxXrooYiIOhzhIV7TuRDbVOlRDak-.xxxxxxxxxx');
sender.send(message2, registrationIds, 4, function(err, result) {
console.log(result);
});**
Then, I ran and recevied message as picture!
According to the documentation, MismatchSenderId means that the sender (API key) does not have access to send notifications to the registration ID:
A registration ID is tied to a certain group of senders. When a client app registers for GCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app. If you switch to a different sender, the existing registration IDs won't work.
You should make sure that your registration ID allows notification from the sender.
I am using node-gcm-service package to send message from my server to the registered devices. When there is one device, the message is successfully delivered, when there are two devices, only the first device (deviceIds[0]) receives the message. Both devices receive messages if they are the only device to which the message is sent or either device is the first device in the deviceIds array. Any ideas what might be going on? Below is the relevant code snippet.
Thank You,
Gary
function(cb) {
var gcmSender = new gcm.Sender();
gcmSender.setAPIKey("api-key");
var gcmMessage = new gcm.Message({
collapse_key: "floomit",
data: {
message:"new_photo",
user:user,
stream:streams[0].name
},
delay_while_idle:true,
time_to_live:34,
dry_run:false
});
gcmSender.sendMessage(
gcmMessage.toString(),
deviceIds,
true,
cb
);
}
Try this snippet hope it'll solve your issue...
i tried with following code it's work fine with all devices.
var gcm = require('node-gcm-service');
function (cb){
var gcmSender = new gcm.Sender();
gcmSender.setAPIKey("google_api_key");
var gcmMessage = new gcm.Message({
collapse_key: "floomit",
data: {
message:"new_photo",
user:user,
stream:streams[0].name
},
delay_while_idle:true,
time_to_live:34,
dry_run:false
});
var registrationIds = [];
registrationIds.push('xxxxxxxxxx1');
registrationIds.push('xxxxxxxxxxx2');
console.info(registrationIds);
for(var i=0;i<registrationIds.length;i++){
gcmSender.sendMessage(gcmMessage.toString(), registrationIds[i], true, function(err, data) {
if (!err) {
// do something
console.info("data",JSON.stringify(data));
} else {
// handle error
console.info("error",JSON.stringify(err));
}
});
}
}
The issue seems to be with the GCM service itself. For 2 devices, the 2nd device eventually received the message, not sure how many hours later. When I had 3 devices, 2 of them received the notification right away, the 3rd one, hasn't received it yet, after more than 30 minutes.