I seem to be following the code as suggested by Google Labs to create serviceWorker and have written the code for pushing notifications, yet it does not seem to be showing up. It seems like the push event from the server does reach the browser, however the showNotification function is just ignored.
// in sw.js
self.addEventListener('push', function(event) {
console.log('Push Received'); // this shows up
event.waitUntil(self.registration.showNotification('Hello World'));
// tried self.registration.showNotification('Hello World') but still does not show up
});
Further to check whether the showNotification works I have put the following in the main.js.
//in main.js
console.log(Notification.permission) // shows granted
if (Notification.permission == 'granted') {
console.log('will show notification') // this gets logged
navigator.serviceWorker.getRegistration().then(function(reg) {
Console.log('reached here'); // this too gets logged
reg.showNotification('Hello world!');
});
Lastly I came across this site that some folks have used for testing
https://gauntface.github.io/simple-push-demo/
as well as
https://web-push-book.gauntface.com/demos/notification-examples/
After permitting notifications from this site, I find no notifications appearing using the screen or the given curl command.
I have tried in both Chrome and Firefox and the results are the same. I am using Chrome 68.x and Firefox 61.x on Mac OS 10.13.6
I find that the site I am using has notifications enabled in the Chrome Settings (advanced section). Even clicking on the secure part of the address bar shows that notifications have been set to allow. Is there some other settings that I am missing? Thanks
It was such a silly error. Mac OS X has a notifications settings which was (I think default) set to 'DO NOT DISTURB`. Disabling that showed the push notifications. Thought other folks, like me, who may not have realised this should know.
Some additional advice for Windows 10 desktop users:
Not a -browser- notification - The notifications caused by self.registration.showNotification will not show up in the browser as you might expect. These notifications show up in the Windows Action Center.
Focus Assist - There is a "Focus Assist" setting in Windows that (analogous to the Apple setting mentioned above) determines whether notifications to the Action Center are silent or result in a toast. It does not mention Action Center.
More than one type of "Notification" Just to confuse things, the Windows "Notifications" area is a different, but related feature. The Action Center has an icon in the Taskbar Notification Area. You can read more here.
Dual Monitors - If you have more than one monitor, you will only see the Action Center icon on the task bar of your primary monitor. Further, you will only see the Action Center toast on the primary monitor.
Existing notifications suppress toasts - If the domain for your site is already listed in the Action Center, you won't see an additional toast for new messages. However, you will still see the item your site created in the Action Center. Clear that item, and your next notification will result in a toast.
Check the MacOS notifications settings -> google chrome -> allow notification
NOTE: only for Macbook users
I was looking for a reason I could not see the notifications either. but In windows I had to go to notification settings and for chrome it was disabled. In the browser allowed notifications was enabled.
Related
I am trying to get the Firebase Cloud Messaging to work. I am just trying a test for now. What I have done is everything in this post:
https://firebase.google.com/docs/cloud-messaging/js/client?authuser=0#retrieve-the-current-registration-token
Then at that end you see this:
messaging.getToken().then((currentToken) => {
I then copy this currentToken and use it as a test by going to Firebase (the website) > Grow (sidebar) > Cloud Messaging (sidebar) > Send your first message (button) > Fill in a 'Notification title' and 'Notification text' > Send test message (blue button to right) > Add the currentToken I copied from above OR check a previously added one > Test (button) ... (see screenshot below)
I assume after clicking the 'Test' button it should send a notification to my phone (where I allow notifications and got the token from). I tested this with my pc and did not get a response either. Using Chrome on my pc and laptop for what that matters.
What am I missing here?
I was missing an important part of adding the gcm_sender_id in the manifest.json. I followed this link exactly and it worked perfectly. You can use the Firebase Cloud Messaging testing platform instead of the Postman approach from the article:
https://codeburst.io/how-to-add-push-notifications-on-firebase-cloud-messaging-to-react-web-app-de7c6f04c920
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);
}
});
``````````
I'm following the Facebook Auth tutorial on the Firebase website. You can see it here: https://www.firebase.com/docs/web/libraries/ionic/guide.html
$scope.login = function() {
Auth.$authWithOAuthRedirect("facebook").then(function(authData) {
// User successfully logged in
}).catch(function(error) {
if (error.code === "TRANSPORT_UNAVAILABLE") {
Auth.$authWithOAuthPopup("facebook").then(function(authData) {
// User successfully logged in. We can log to the console
// since we’re using a popup here
console.log(authData);
});
} else {
// Another error occurred
console.log(error);
}
});
};
My issue is that I am correctly receiving the TRANSPORT_UNAVAILABLE error and I am getting to the following line of code
Auth.$authWithOAuthPopup("facebook").then(function(authData) {
// do stuff with the authData
})
But, when I run on my device or in emulator, the popup window that is coming from the InAppBrowser Plugin closes immediately and doesn't allow me to enter any of my credentials.
EDIT
Two things to note. First, with the above code auth does not work when done via the browser. So, if I do ionic serve and try to login nothing happens except that I see the url change briefly to http://localhost:8100/#/login&__firebase_request_key=0wRrfF07Ojg1PmJXNX1OsvrRFR2Q1LGj
but then it goes back to http://localhost:8100/#/login
Secondly, when I build the project via Xocde and run on my device, the InAppBrowser plugin seems to no longer be closing right away but instead freezes with a white screen. The logs in Xcode show the following
THREAD WARNING: ['InAppBrowser'] took '79.103027' ms. Plugin should use a background thread.
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
webView:didFailLoadWithError - -1200: An SSL error has occurred and a secure connection to the server cannot be made.
EDIT 2
Looks like the above issues with SSL error was because of an unrelated bug with upgrading to ios 9. I've since corrected those issues and now I'm back to the original. Except now the InAppBrowser window doesn't even open, I'm still hitting the catch block with TRANSPORT_UNAVAILABLE.
Not sure exactly how I fixed this issue. Hard to isolate what was breaking originally and what was breaking due to ios 9 upgrades. But, I've been able to fix the issue. I started by blowing away the /ios and /android folders inside of /platforms. I also deleted all the plugins from the /plugins folder.
Then I added back ios and android platforms. Then I added back the plugins. Then I followed the steps found in these 2 blog posts modifying your app to be ios 9 compliment.
http://blog.ionic.io/ios-9-potential-breaking-change/
http://blog.ionic.io/preparing-for-ios-9/
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!"
}
I have tested to send push notifications with cordova-1.8.1.js and the push plugin together with pushwoosh.com and it work as it should.
I followed this tutorial: http://www.pushwoosh.com/programming-push-notification/push-notification-sdk-integration-for-phonegap/
The push notification is send to my iPhone and it plays the sound and shows the notification when the phone and app is closed, good!
But if I open the phone when the notification is visible then the app is opened as it should...but
the alert that is displayed is saying:
Alert
"push-notification","{\aps\":\sound\":\"default\",\"alert\":\" and then the message....\"}}”
So what is wrong, it should only write the message in the alert and not the rest?
Also if I delete the app with home button and start it again I get another alert saying "registerDevice", "type":"7".....and so on.
2. How can I make this go away?
Any input appeciated, thanks!
Problem solved. Use this and it will only show the message in the alert and nothing else.
document.addEventListener('push-notification', function(event) {
//console.warn('push-notification!: ' + event.notification);
//navigator.notification.alert(JSON.stringify(['push-notification1!', event.notification]));
var notification = JSON.parse(event.notification);
navigator.notification.alert(notification.aps.alert);
//pushNotification.setApplicationIconBadgeNumber(0);
pushNotification.setApplicationIconBadgeNumber(0);
});