Add new Subscription to Rocket chat - meteor

I want to create a new subscription e.g (stream-all-messages) in Rocket Chat same as stream-room-messages which is already present in docs.
I am using DDP listener to test it, following are the examples which I am trying.
// Subscribe to a message stream from a channel or group
console.log("Attempting to subscribe to the Group/Channel now.\n");
ddpClient.subscribe("stream-room-messages", [RoomId, false], function() {
console.log(ddpClient.collections);
console.log("Subscription Complete.\n");
// Display the stream on console so we can see its working
console.log("\nStarting live-stream of messages.:\n");
ddpClient.on("message", function(msg) {
console.log("Subscription Msg: " + msg);
});
});
above example works because stream-room-messages already present but when I try to execute the following it raises an error i.e.
Subscription Msg: {"msg":"nosub","id":"2","error":{"isClientSafe":true,"error":404,"reason":"Subscription 'stream-only-messages' not found","message":"Subscription 'stream-all-messages' not found [404]","errorType":"Meteor.Error"}}
when I call this
// Subscribe to a message stream from a channel or group
console.log("Attempting to subscribe to the Group/Channel now.\n");
ddpClient.subscribe("stream-all-messages", [RoomId, false], function() {
console.log(ddpClient.collections);
console.log("Subscription Complete.\n");
// Display the stream on console so we can see its working
console.log("\nStarting live-stream of messages.:\n");
ddpClient.on("message", function(msg) {
console.log("Subscription Msg: " + msg);
});
});
Please help me out how can I add a new subscription.

Related

Firebase - notificationclick event not receiving payload

I'm struggling with this problem for more than a week... We have implemented push message in Chrome by using Firebase and a Service Worker. Everything works just fine, the messages are being sent and received correctly with the payload. On the service worker, we handle the push message to display a notification and the notificationclick event to send the user to a specific URL when clicking on it, then close the notification.
The problem is with 'old' notifications: if a user receives a message but doesn't clicks on it right away, it keeps there for a while then after some time (not sure how much) he clicks the notification - he gets redirected to https://[domain]/firebase-messaging-sw.js
We have traced the entire process: the notification gets received with all the info properly (the url is also correct, actually if he clicks right when the message is received it works just fine). But if the notification lives there for a while it gets 'emptied'.
The message being sent is pretty simple (just for showing there are no TTLs, nor expiration parameters being used). The curl command looks like that:
curl -X POST
-H "Authorization: key=[SERVER API KEY]"
-H "Content-Type: application/json"
-d '{
"notification":{
"click_action":"[URL to be redirected]",
"icon":"[A custom image]",
"title":"[News title]"},
"to":"/topics/[A topic]"
}'
"https://fcm.googleapis.com/fcm/send"
This is the code for processing the push message on the service worker:
self.addEventListener('push', function(event) {
console.log('Push message received', event);
var data = {};
if (event.data) {
data = event.data.json();
}
event.stopImmediatePropagation();
showNotification(data.notification);
});
function showNotification(notification) {
var click_action = notification.click_action; //<-- This is correct!
var options = {
body: notification.body,
icon: notification.icon,
subtitle: notification.subtitle,
data: {
url: click_action
}
};
if (self.registration.showNotification) {
return self.registration.showNotification(notification.title, options);
}
}
And the code for managing notificationclick event is pretty straightforward:
self.addEventListener('notificationclick', function(event) {
var url = '';
try {
url = event.notification.data.url; // <-- event.notification is null randomly!!
} catch (err) {}
event.waitUntil(self.clients.openWindow(url));
});
Is there any reason for loosing the payload after a certain time on the service worker context? Is this time specified on any documentation somewhere?
Thanks in advance for your help!
//payload of push message
{
"notification": {
"title": data.title,
"body": data.text,
"click_action": url,
"tag": "",
"icon": ""
},
"to": token
}
//in service woker
self.addEventListener("notificationclick", (event) => {
event.waitUntil(async function () {
const allClients = await clients.matchAll({
includeUncontrolled: true
});
let chatClient;
for (const client of allClients) {
if (client['url'].indexOf(event.notification.data.FCM_MSG.notification.click_action) >= 0) {
client.focus();
chatClient = client;
break;
}
}
if (!chatClient) {
chatClient = await clients.openWindow(event.notification.data.FCM_MSG.notification.click_action);
}
}());
});
so in this basically, on click of notification you are redirected to application ,and if the application is not open you are opening the application in new tab , as you are concerned that in event you are not able to get the click_action, could you try event.notification.data.FCM_MSG.notification.click_action by using this and see if you are getting the url, and add the event listener in beginning of service worker https://github.com/firebase/quickstart-js/issues/102

Parse Server Cloud code keeps sending push notifications

I've developed an app that sends push notifications using Parse Server Cloud code. These notifications are received correctly in the devices but hours later they are automatically sent from Parse Server again (and they are received again). This happens 3 or 4 times for each push notifications.
If push notifications are sent from Parse Dashboard they are only sent once, so it seems it's a problem of my cloud code.
This is my code:
Parse.Cloud.define("sendPushNotification", function(request, response) {
var userId = request.params.userId;
var message = request.params.message;
var queryUser = new Parse.Query(Parse.User);
queryUser.equalTo('objectId', userId);
var query = new Parse.Query(Parse.Installation);
query.matchesQuery('user', queryUser);
Parse.Push.send({
where: query,
data: {
alert: message,
badge: 0,
sound: 'default'
}
}, {
success: function() {
console.log('##### PUSH OK');
response.success();
},
error: function(error) {
console.log('##### PUSH ERROR');
response.error('ERROR');
},
useMasterKey: true
});
});
I had a similar issue sending emails from another cloud code function (not included in the question) and my problem was because I forgot to add response.success(); and response.error('ERROR'); methods.
So this time I was sure to include these 2 calls in the responses of "sendPushNotification" method.
After sending a push notification the logs show this:
2017-07-09T15:38:02.427Z - Ran cloud function sendPushNotification for user undefined with:
Input: {"message":"This is my message","userId":"myUserIdInParse"}
Result: undefined
I think that this "Result: undefined" could be related with the problem because success and error functions are not called.
What could be the problem with this code? Why the code doesn't receive a success() when the notifications are received correctly in the devices?

Nativescript. nativescript-local-notifications always creates two notifications

I use nativescript-local-notifications and nativescript-plugin-firebase.
I want to create notification each time, when server send me a message. Here is code:
firebase.init({
storageBucket: 'gs://shao-by-partner-firebase.appspot.com',
persist: true, // optional, default false
onPushTokenReceivedCallback: function (token) {
Config.device_token = token;
console.log("Firebase plugin received a push token: " + token);
},
onMessageReceivedCallback: function (message) {
LocalNotifications.schedule([{
id: 1,
title: 'The title',
body: 'Recurs every minute until cancelled',
ticker: 'The ticker',
badge: 1,
smallIcon: 'res://heart.png'
}]).then(
function() {
console.log("Notification scheduled");
},
function(error) {
console.log("scheduling error: " + error);
}
)
}
}).then(
function (result) {
console.log("Firebase is ready");
},
function (error) {
console.log("firebase.init error: " + error);
}
);
}
But always created two notifications instead of one. And first notification is empty. And second notification is that I created. What's wrong?
I could be wrong here, but you are using two plugins to handle remote notifications?
nativescript-plugin-firebase already handles notifications for you, I don't think you need the local notifications plugin.
In the example you posted, you are receiving the original notification (the blank one), pluss the local one that you create in the onMessageReceivedCallback.
https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#handling-a-notification
The is nothing in your onMessageReceived to determine if the message had any content.
You can try adding a conditional statement to your onMessageReceived function and check if the message has content. eg
if (message.title){
//send local notification
}
I found solution. Each time, when I create LocalNotification, I delete notification with id 0:
LocalNotifications.cancel (0);
But I still don't know,where it comes from.

Integration of node-whatsapi in meteor

I want to be able to nose whatsapi in meteor. I am using
latest stable meteor
node-whatsapi
arunoda´s meteorhacks:npm
and can get the past the basics:
On meteor server startup, I have:
whatsapi = Meteor.npmRequire('whatsapi');
wa = whatsapi.createAdapter({
msisdn: '....',
username: '....',
password: '....',
ccode: '....'
});
wa.connect(function connected(err) {
if (err) {console.log(err); return;}
console.log('Connected');
wa.login(logged);
});
function logged(err) {
if (err) {console.log(err); return;}
console.log('Logged in');
wa.sendIsOnline();
};
... which let me send and receive messages with a method call to
wa.sendMessage(recipient, content, function(err, id) {
if (err) {console.log(err.message); return;}
console.log('Server received message %s', id);
});
The code bellow also works, logging received messages on the console. This sits inside server Meteor.startup:
wa.on('receivedMessage', function(message) {
console.log("From: " + message.from);
console.log(message.body);
});
My problem is that when I try to add store message.from or message.body into a collection, meteor gives me "Meteor code must always run within a Fiber" error")
wa.on('receivedMessage', function(message) {
console.log("From: " + message.from);
console.log(message.body);
Recipients.insert({msgfrom: message.from});
});
Help!
Use Meteor.bindEnvironment to wrap any callback given out by your npm module. It will wrap the callback into a 'Fiber' so you can run Meteor code in it.
For example:
wa.on('receivedMessage', Meteor.bindEnvironment(function(message) {
console.log("From: " + message.from);
console.log(message.body);
Recipients.insert({msgfrom: message.from});
}));
What it does essentially is place the code in the callback into a Fiber.

From which file do I send a push notification in strongloop?

I am trying to set up push notifications with strongloop. I don't understand which file the code below lives in. The docs don't say, which is confusing for newbies.
I understand that I have to add a push component to loopback restful api application, which I have done. But how do I reference the push component from my restful api app? Where's the 'glue'?
http://docs.strongloop.com/display/public/LB/Push+notifications
var badge = 1;
app.post('/notify/:id', function (req, res, next) {
var note = new Notification({
expirationInterval: 3600, // Expires 1 hour from now.
badge: badge++,
sound: 'ping.aiff',
alert: '\uD83D\uDCE7 \u2709 ' + 'Hello',
messageFrom: 'Ray'
});
PushModel.notifyById(req.params.id, note, function(err) {
if (err) {
// let the default error handling middleware
// report the error in an appropriate way
return next(err);
}
console.log('pushing notification to %j', req.params.id);
res.send(200, 'OK');
});
});
This should live in the app.js file as seen in the example here:
https://github.com/strongloop/loopback-component-push/blob/master/example/server/app.js#L137-L145

Resources