This is complicated but I'll try to be as concise as possible. I am using FCM to send push notifications to Android devices. My backend is parse server. I can successfully send pushes from the FCM console to the device. And when I send pushes from parse to https://mybackend/parse/push, it says {"result": "true" } as the response. I have checked the server logs, and it (predictably) says all http post requests to /parse/push were 200 success.
However when sending from parse (using curl or the parse push console), the pushes do NOT show up in FCM console. There is some issue between parse server and FCM then.
In my index.js, I have:
push: {
android: {
senderId: "XXXXX",
apiKey: "YYYYYY"
}
}
I have the keys, and done everything according to here: http://parseplatform.org/docs/parse-server/guide/#push-notifications
what else am I missing? Why can't I get this to actually appear on the device? Any help is greatly appreciated, thanks.
Setting up a Parse Server for an Android app, I remember having to double check this to get it right. The key for me was this: when I logged into Firebase console and selected the app I was working on, and clicked on the gear [or cog or whatever] and then Project Settings, I had to make sure to click on the Cloud Messaging tab and grab the Legacy server key [under project credentials] NOT the web api key that appears on the General tab. Also the sender ID that you want to use appears on that page.
I'm not sure when they will deprecate the Legacy server key, you might try the Server key instead, but definitely make sure you get the info off of the Cloud Messaging tab.
I don't know why FCM (and APNs) don't send a different message when the key (or cert) is not set up right [that would be more developer-friendly at least] and instead just send a 200 but my guess is that they evaluate the key [or cert] after receiving the intended notification and sending a response.
Related
I'm making an Android app in which I want user to sign in to their Outlook account and receive push notifications to the app from the Microsoft Graph API when an email is received in their inbox. How can I do this?
I can subscribe to inbox changes using a HTTP subscription request (as specified here https://learn.microsoft.com/en-us/graph/webhooks?view=graph-rest-1.0), with something like:
POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
"changeType": "updated",
"notificationUrl":
"https://webhook.azurewebsites.net/notificationClient",
"resource": "/me/mailfolders('inbox')/messages",
"expirationDateTime": "2016-03-20T11:00:00.0000000Z",
"clientState": "SecretClientState"
}
In this request I need to specify a "notificationUrl" where notification updates are sent to - how can I set this up? Is there functionality for this on Azure?
From there I believe I can use the instructions here to send push notifications to the Android device https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-android-push-notification-google-fcm-get-started.
This involves setting up a notification hub on Azure which connects to Firebase, which then sends notifications the app. Is this the best/only way to do this?
Any help much appreciated!
The notificationUrl can be the webhook url of the azure function app. https://azure.microsoft.com/en-us/resources/videos/create-a-web-hook-or-api-azure-function/.
Therefore, you can use azure function to invoke Firebase API to send notifications.
Please see https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-notification-hubs#packages---functions-1x and https://firebase.google.com/docs/cloud-messaging/migrate-v1.
Besides, I would recommend you to use azure logic app. It has built in connector to use when a message arrives your inbox.
Take a look here https://learn.microsoft.com/en-us/azure/connectors/connectors-create-api-office365-outlook.
I integrated the push notification system with Firebase in my project and it's working well except one thing.
If I try to send a notification from Firebase console, using Device Token & Topic, the notification shows in device.
If I try to send a notification from ARC ( Advance REST CLIENT) using Device Token, the notification shows in device.
If I try to send a notification from ARC ( Advance REST CLIENT) using my topic, NOTHING HAPPENS.
Requested URL : https://fcm.googleapis.com/fcm/send
RAW headers :
Content-Type: application/json
Authorization: key=AIz.....
RAW PAYLOAD :
{
"data":
{
"employeename":"kumaran","empid":"234444"
},
"to":"/topics/bmmembers"
}
Response Header
Status : 200: OK
JSON :
{
"message_id": 6536474446058224000
}
But my device not received any notification and checked Logcat whether any json arrived in client end, but not received.
Any idea and how we need to resolve this issues.
This is my trial and error type of a "Solution found for this Issue":-
Issue found is "The Quota for the particular Server-key or credential or the whole Google API Account itself might have been expired or used the limit. "
Below i have explained how i once again successfully received FCM Notifiaction with Image for the same Android App.
ISSUE:-
I was unable to send FCM push notification (with image) from Advanced REST Client, by using the Android key generated from Google API console in my very old created Project (Eg: My Project; that has 5-apps and its credentials).
I tried sending the Post request from ARC client many time but dint receive any single FCM notification in my Android Mobile.
SOLUTION I FOUND WAS:-
1a. I deleted the credentials (API key, server key,web key, OAuth key etc) in Google API console related to that particular app (Eg: FCM App1).
1b. Also i went to Firebase console and deleted the particular app in its project also.
Finally now the App (Eg.FCM App1) doesn't contain connection with
1.Google API console and with
2.Firebase Console.
1c. I also deleted the "google-services.json" file from the Android studio->Project folder.(If you are unable to delete this, try executing File->'Invalidate cache and restart' option and then retry deleting it. It will get deleted.)
Next, freshly
I created a new gmail account, and opened a new Google API Account, further opened a fresh new Project in that Google API Account (Eg: My Project1).
Next, I went to Firebase console signing in with this newly created Gmail acccount, opened a fresh new Project, selected "Add App" and selected "Android".
-> entered my package name, SHA1 key and Added the App.
-> Also downloadedthe new google-services.json file and added again in the Project folder in Android Studio.
-> And followed everything as per previous procedure.
==> Finally I got the fresh ""Server-Key"" in the new Google API console. Copied this server-key and pasted in Advanced REST client at place Authorization:key=AIzaSyAJzs-DDD......etc.
=> added the correct TokenID freshly created for this App from Log.
===> Pressed SEND button in the ARC, and WOOOOWWWW....
within few seconds I GOT THE FCM NOTIFICATION WITH IMAGE IN MY ANDROID MOBILE EVENTHOUGH MY APP WAS IN BACKGROUND OR COMPLETELY CLOSED at that time....!!!
I am using AWS resources for my android project, I am planning to add push notification service for my project with AWS SNS.there are few questions bothering me much. I did not find any questions regarding these, except one or two but with unclear explanations.
1.Does AWS support FCM? SNS work with GCM. But Google recommends to use FCM instead of GCM. I did not find AWS supporting FCM.
2.Do AWS store messages (or data) into their databases even after sending push notifications?
3.I tried putting FCM api key in SNS application platform, it is showing invalid parameters why?
FCM is backwards compatible with GCM. The steps for setting up FCM on AWS are identical to the GCM set up procedure and (at least for the moment) FCM works transparently with GCM and SNS with respect to server-side configuration.
However, if you are sending data payloads to the Android device they will not be processed unless you implement a client side service that extends FirebaseMessagingService. The default JSON message generator in the AWS console sends data messages, which will be ignored by your app unless the aforementioned service is implemented. To get around this for initial testing you can provide a custom notification payload which will be received by your device (as long as your app is not in the foreground)
There are GCM-FCM migration instructions provided by Google however the changes you need to make are predominantly on the App side.
The steps you need to follow to test GCM/FCM on your app with SNS are:
Create a Platform Application in SNS, selecting Google Cloud Messaging (GCM) as the Push Notification Platform, and providing your Server API key in the API key field.
Select the Platform Application and click the Create platform endpoint button.
Provide the InstanceID (Device Token) generated by your app. You must extend the FirebaseInstanceIDService and override the onTokenRefresh method to see this within your Android App. Once you have done this, uninstall and reinstall your app and your token should be printed to the Debug console in Android Studio on first boot.
Click the Add endpoint button.
Click on the ARN link for your platform application.
Select the newly created Endpoint for your device and click the Publish to endpoint button.
Select the JSON Message Format, and click the JSON message generator button.
Enter a test message and click the Generate JSON button
Now comes the "gotcha part".
The message that is generated by SNS will be of the form:
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
As we mentioned earlier, data payloads will be ignored if no service to receive them has been implemented. We would like to test without writing too much code, so instead we should send a notification payload. To do this, simply change the JSON message to read:
{
"GCM": "{ \"notification\": { \"title\": \"test title\", \"body\": \"test body\" } }"
}
(For more information about the JSON format of an FCM message, see the FCM documentation.)
Once you have done this, make sure your app is not running on the device, and hit the Publish Message button. You should now see a notification pop up on your device.
You can of course do all this programmatically through the Amazon SNS API, however all the examples seem to use the data payload so you need to keep that in mind and generate a payload appropriate to your use case.
Now you can go to your firebase console (https://console.firebase.google.com/) select your project, click the gear icon and choose project settings, then click on the cloud messaging tab...
You'll see the legacy Server Key which is the GCM API Key and you'll have the option to generate new Server Keys which are the FCM versions
SNS will accept both versions but their menu option is still categorizing it under GCM
Here is picture for your reference:
Note that you can "accidentally" remove your Server Keys but the Legacy server key is not deletable. Also, if you click the add server key button, you'll get a new server key BELOW the first one, WITH NO WARNING! ...Nice job Google ;)
One more additional note to Nathan Dunn's great answer.
How to send data with the notification from SNS to Firebase.
We need to add data to the Json (inside the notification):
{
"default": “any value",
"GCM": "{ \"notification\": { \"body\": \”message body\”, \”title\”: \”message title \”, \"sound\":\"default\" } , \"data\" : {\”key\" : \”value\", \”key2\" : \”value\” } }”
}
In your FirebaseMessagingService implementation (Xamarin example)
public override void OnMessageReceived(RemoteMessage message)
{
try
{
var body = message?.GetNotification()?.Body;
var title = message?.GetNotification()?.Title;
var tag = message?.GetNotification()?.Tag;
var sound = message?.GetNotification()?.Sound;
var data = message?.Data
foreach (string key in data.Keys)
{
// get your data values here
}
}
catch (Exception e)
{
}
}
I tried to use solution with notification payload instead of data, but I did not receive push notifications on the mobile device. I found this tutorial https://youtu.be/iBTFLu30dSg with English subtitles of how to use FCM with AWS SNS step by step and example of how to send push notifications from AWS console and implement it on php with aws php sdk. It helped me a lot.
Just an additional note to Nathan Dunn's Answer: to add sound use the following JSON message
{
"GCM": "{ \"notification\": { \"text\": \"test message\",\"sound\":\"default\" } }"
}
It took me a while to figure out how to send the notification with the right payload (publish to topic). So I will put it here.
private void PublishToTopic(string topicArn)
{
AmazonSimpleNotificationServiceClient snsClient =
new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.EUWest1);
PublishRequest publishRequest = new PublishRequest();
publishRequest.TopicArn = topicArn;
publishRequest.MessageStructure = "json";
string payload = "\\\"data\\\":{\\\"text\\\":\\\"Test \\\"}";
publishRequest.Message = "{\"default\": \"default\",\"GCM\":\"{" + payload + "}\"}";
PublishResponse publishResult = snsClient.Publish(publishRequest);
}
Amazon does support FCM as all previous code has been migrated from GCM to FCM. Below article explains in detail.
Article Published by Amazon
To answer the questions:
AWS SNS does support FCM.
No AWS does not store messages after sending push notifications.
For a detailed tutorial on setting up FCM with SNS please read this article.
I'm trying to get my app engine project to send out gcm messages. I've gone through the project walkthrough, but am not sure what kind of key I need. I think I need a "browser" key:
My app engine project has a url like "https://example.appspot.com/". When creating a browser key, what should I enter for the "Accept requests from these HTTP referers (web sites)" section? They give an example of:
*.example.com/*
so I'm trying:
*.example.appspot.com/*
But I still get a 401 Unauthorized response when trying to send:
Sender sender = new Sender("myBrowserApiKey");
sender.sendNoRetry(...);
Right, does anyone know what we're supposed to be using here? I don't think the other key types (Server key, Android key, iOS key) are what I want, because my app engine project doesn't have a static ip, nor am I sending messages directly from my android or ios clients.
Thanks
http://developer.android.com/google/gcm/http.html
http://developer.android.com/google/gcm/gs.html
-------- Update ---------------
I went ahead and tried "Server key", and left the whitelists empty. When you save the key with the empty whitelist, you'll see it display "Any IP allowed". After saving, the messages started going through ok about 15 minutes later.
So I guess "server key" is the right one to use if you've got the same setup as me.
I have a Json URL, which contains data about Latest Job Postings, I am successfully parsing the Json URL and able to display the top job postings in my ListView.
But my requirement is to create a push notification, so that whenever a new job is posted, the user should be able to get a notification on device.
I have followed this: http://www.vogella.com/articles/AndroidNotifications/article.html
But I don't know how to get notifications in my case.
Could anyone help me?
Issue:
Give push notification to user's device about the updated data even when application is in background mode.
Solution:
Upon successful insertion of new data in your database (which is going to give updated set of data to your JSON request) , just call the file which send GCM push notification to all your users.
Reference:
GCM docs
GCM push-notification using php server
In context of implementation presented in demo app of 2nd link,
upon successful insertion,you can call send_message.php file,but make sure that $regId and $message should be retrieved from your database
You have created ActionBar Notifications for your app, but now you need to create the ability to receive notifications from a web client, instead of going to find them yourself from the URL.
To create a push notification you would need to have a constant thread (BroadcastReceiver) on the device that is waiting for the notification from the sever.
Google 'Cloud to Device Messaging' is the simplest way to do this.
This is a good link with lots of info on how to do this :
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/
If you require these notifications to be displayed on the device even when the application is not running (which seems to be the case from what you describe), you can use Google Cloud Messaging.
You would need a server that would poll the Json URL for updates, and send a GCM message to all the devices where your app is installed once such an update is detected.
Your app would have to register to Google Cloud Messaging and send the Registration ID received from Google to your server.
When your app receive a GCM message, you would create a notification and when the notification is tapped, you would start the activity that loads the data from the JSON URL.