Can you expire / remove a message remotely on FCM? - firebase

I'm using FCM for my push messages. Its great, however I've a client that needs to be able to remove a push message once its sent - (in case of mistakes / typos etc).
I know you can get the message id from when the Topic is queued to send messages, just wondering if there is a way to then use that ID to expire those messages remotely. i.e. to delete the message.

There is currently no way to delete/remove a message from the server side/console. The message_id is just an identifier that the message was sent successfully to the FCM server.
What is usually used for this scenario is the tag parameter (see my answer here) where an existing notification with the same tag gets replaced with the newer one.

There are scenarios where you might want a replacing notification to notify the user rather than silently update. Chat applications are a good example. In this case you should set tag and renotify to true.
write this code on your sw.js
const title = 'Notification 2 of 2';
const options = {
tag: 'renotify',
renotify: true
};
registration.showNotification(title, options);
You can test demo on here by clicking in renotify button

Related

Firebase push notifications always arrive as an empty message

I have been trying to get push notifications working using firebase. So far I have got as far as successfully sending an empty message "tickle". The problem is adding the message payload seems to have no affect on what the client receives. That is the service worker just sees it as another empty message.
I started by going through googles guide here - https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications
After going through how to send an empty message it says the message payload must be encrypted and suggests using an existing library to do it. To quote - "As with anything related to encryption, it's usually easier to use an actively maintained library than to write your own code".
I tried to use web-push-php which is one of the libraries recommended by googles guide. After having trouble with that i discovered web-php-push doesn't actually support firebase.
Looking on here i find examples that look really simple and don't event encrypt the message payload. It is simply sent in plain json. Doing this has no affect and the receiving end still thinks it's an empty message. See my code below.
I am at a complete loss with this and i'm confused why googles guide says the message data must be encrypted but there are countless examples on SO where it is just send in plain json text.
This is what i am posting from my server to the end point.
POST https://fcm.googleapis.com/fcm/send Authorization: key=[my server
key] Content-Type: application/json {"priority":10,"to":"[subscriber
id]","notification":{"body":"test body","title":"test title"}}
Here is my event listener in my service-worker.js
self.addEventListener('push', function(e) {
var body;
if (e.data) {
body = e.data.text();
} else {
body = "No message "+JSON.stringify(e);
}
var options = {
body: body
};
e.waitUntil(
self.registration.showNotification('Launtel Residential', options)
);
});
When i run the post request above the push notification occurs and triggers the service worker 'push' event as expected but no message data is present. e.data returns null. The 'e' object always just contains a flag set to true. e.isTrusted==true

Messages successfully sent, but no notifications visible in Firebase Cloud Messaging

Messages are reported as successfully sent, but no pop-up notifications appear.
With onMessage it is sometimes possible to log the message.
Requesting an FCM token only works on localhost.
When tried on the .com site, the error message depends on whether I use the PC or chromebook - the error messages generated are different.
I have used 2 different devices, but always Chrome browser. The cloud function writes the message and the FCM response to the function log, so it confirms success in sending.
When I deleted onMessage from the recipient webpage there was no sign of any notification.
The webpage is open during these trials. This is web/javascript.
Notification permission is set to allow, in both devices.
I have spent hours on web searches trying to find any hint as to what to try.
firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': '593287500713'
});
const messaging = firebase.messaging();
the relevant code which runs with the webpage
messaging.onMessage(function (payload) {
console.log('Message received. ', payload);
});
I assume that without onMessage, the notifications should appear based on the Firebase SDK, but they don't. WRONG ASSUMPTION
When in the background, nothing appears. (I think notifications are supposed to appear automatically.)
I can request and receive an FCM token when running on local host but the same code throws an error when deployed. "Request is missing required Authentication..."
I have been in contact with support # Google who have assured me that this suggestion, and then another would 100% fix the problem. Nothing made any difference.
I have tried the code snippets in the Firebase docs and also github examples.
If I could find existing code that works, that would be a great start, because I think that finding a solution after about 40 hours of effort is hopeless.
Have I missed something obvious?
UPDATES....
I have found two reasons for not seeing notifications.
1) My PC had a setting which seems to have partially suppressed the display. Unexpected because YouTube notifications do display. (Open notification tray on far right bottom of screen. "focus assist" with a crescent moon symbol. If when clicked it says "on" or "alarms only" I think this restricts what is seen.)
2) My assumption based on the Firebase docs and video was that onMessage() is to prevent notifications when the user is on the webpage and to allow the developer to ignore or handle the notifications within the page. Half right. Apparently the notifications are default ignored UNLESS onMessage() does something with them. The docs give snippets of code for onMessage() but only how to log to the console.
Also, found an amusing problem. Now that my PC has registered the recipient of the messages, it receives them even if that recipient is signed out and has signed in on another device. That sounds like a problem.
The currently working version of the code. This goes in the webpage / app (NOT in the messaging-sw.js file
The introductory video to messaging suggests that it is bad UX to have notifications from a website popping up when someone is on the website. Therefore the code can include onMessage() to either ignore the notification or to handle it within the page.
The docs give a version of onMessage that just logs the notification to the console and has just a comment about doing something else.
Other parts of the docs explain that the system automatically displays notifications.
Therefore I inferred that without onMessage() the notifications would appear automatically.
It seems I was wrong.
It has taken me a massive amount of time to figure this out and to find some code to make the notifications appear. (Even now I am not sure I have grasped what is happening, but the following seems to work)
This goes in the web page / app. (Not in the messaging-sw.js. Putting it there will throw an error "using window methods")
[Do note that I also found my PC was set in a way that seemed to allow YouTube to send notifications, but was preventing the display of FCM notifications. (See explanation in question)
So far I have tested this when the use has the webpage open, the webpage in the background and when the browser is closed. Notifications appear with the default sound and are listed in the notification tray.
This also happens if the recipient is signed out of the webpage and signed in on another device.. the messages continue to go to the same device.
Messages go to devices not to users]
messaging.onMessage(function (payload) {
try{ //try???
console.log('Message received. ', payload);
noteTitle = payload.notification.title;
noteOptions = {
body: payload.notification.body,
icon: "typewriter.jpg", //this is my image in my public folder
};
console.log("title ",noteTitle, " ", payload.notification.body);
//var notification = //examples include this, seems not needed
new Notification(noteTitle, noteOptions);//This can be used to generate a local notification, without an incoming message. noteOptions has to be an object
}
catch(err){
console.log('Caught error: ',err);
}
});
``````````

Firebase messaging failing on flutter after some time

I'm having an issue with FCM on flutter. I have implemented messaging from my server so I'm storing my phone token for each user.
The thing is that when a user logs in for the very first time everything works properly, messages are being sent and user gets notified.
If I do not use the app during the weekend, on Monday I try to send a message by doing some actions on my app but messages are not being sent. I can see my token stored properly in my database.
I'm using firebase_messaging 2.1.0 for flutter.
This is how I get my token
_fireBaseMessaging.getToken().then((token){
_myPhoneToken = token;
});
1-I know token may change when:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
But none of this happens.
Any advice on how to handle this scenario? thanks in advance.
UPDATE
Provided you have setup the FCM sdk the right way (but you said that it works the fist time you install the app, so I guess so).
Provided that you are sure that the device_token you are using is the one of the device on which you are expecting to receive the notification (check if it's still the same), you should get on this device your notification quite soon if you use "priority" : "high".
{
"to" : "device_token",
"priority" : "high",
"notification" : {
"sound": "default",
"body" : "Test Notification body",
"title": "Test Notification title"
}
}
This method call
_firebaseMessaging.getToken().then((String token)
return always the new token even if it has been updated. So if you print this out on your device and you send a notification on this token without error, there's no reason why you should not get the token if the device has a valid internet connection active.
It's true that the device token can change during time. If you uninstall and reinstall the app, you can see the token will change and if you try to send a notification on the old one, you will get an error.
If instead the token will change during application lifetime, you can be notify on your server side by listening:
_firebaseMessaging.onTokenRefresh.listen((newToken) {
_fcm_token = newToken;
// send the new fcm to your server
});
So first of all I suggest you to be able to send a notification to a device with Postman. Check if the token you are using is still the one on the device. Then you can try to uninstall and reinstall the application and try to use the old token. You will get an error. Then try to send to the new one, and you should get your notification.
Then wait for some days and try again, check if the token has changed or not and if it's not changed you should be able to send the notification without problems with the same token.
Also be aware that data message on Android if the app is terminated are still not supported.
Some networks/router/mobile can cut the connection between firebase library and firebase server due to inactivity (5min without message). This cut may be detected by the library up to 30min (FCM heatbeat interval).
These are some links discussing this issue:
https://github.com/firebase/quickstart-android/issues/307
Android: Delay in Receiving message in FCM(onMessageReceived)
I contacted firebase support but they told that since the issue is caused by external part they cannot fix it (I suggest decreasing heartbeat interval ...)
I fixed it in android using an interval job which apply these instructions:
context.sendBroadcast(new Intent("com.google.android.intent.action.GTALK_HEARTBEAT"));
context.sendBroadcast(new Intent("com.google.android.intent.action.MCS_HEARTBEAT"));
You may write this specific code for Android side and should find something similar for ios side.

Push Notifications on iPhone without alerts to the user

I have searched the web on my question but did not find anyone answering it. This looks weird as I am sure other people face similar issue.
At the moment my app is receiving push notification fine. I have a chat module where user can speak and whenever a new message is being sent, the other phone receive a push notification to update the chat.
You could say no issue there, but the problem is when the user is out of the application: he is still receiving those notifications showing a banner on the screen, and I want to dis-activate this. Basically I want push notification without alerts to the user. Is there a way to do that?
Thanks
Just leave the sound property of your push notification payload empty, omit the alert/text property and add "content-available":1 and your notification will be silent. This is often referred to as silent push notification or "push-to-sync".
See documentation here:
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app
So your payload should least look like this:
{
"aps" : {
"content-available" : 1,
"sound" : ""
},
"chat-message" : "Hello World!"
}

Invalid tag for push notifications in Windows Azure

I am building a Windows Phone 8.1 application and want to add push notifications from Windows Azure. I am creating the channel by using CreatePushNotificationChannelForApplicationAsync, after which I take the resulting URI and store it in the Azure database. When trying to send a push notification by using push.wns.sendToastText01, I get the following error in the Azure logs:
Error in script '/table/Message.insert.js'. Error: 400 - An invalid tag 'https://db3.notify.windows.com/?token=AwYAAAC3tTi3W5ItZ0hWdZ3FLmELt%2flHcwpsM...' was supplied. Valid tag characters are alphanumeric, _, #, -, ., : and #.
I noticed that the channel URI contains the '%' which does not appear among the valid characters, yet that is the URI that gets generated in the client application. Am I using a wrong method for sending push notifications or is there something else I am missing?
Edit: I am using Node.js for backend in Azure.
request.execute({
success: function() {
push.wns.sendToastText01(channelUri, {
text1: "Google Plus Friend Tracker",
text2: item.content,
param: '/ChatPage.xaml?friendGoogleId=' + item.author_id
})
}
});
Looking at the wns object documentation, the first parameter would be the tags that you are sending to. Since you're providing a channel in the code above, you are getting the error specified.
The backend does not need to provide the channel URI, as this was associated with the Notification Hub via the client-side registration action. If you are broadcasting the message, you would just provide null as the tag value. Otherwise, you can use the tags that were specified when you registered the channel URI.
For more about the process, see the "Get started with push" tutorial. There is also an example of using a tag (user ID) in the "Send push notifications to authenticated users" tutorial. For more on tags in general, the Notification Hubs breaking news tutorial is also good.

Resources