How to post notifications to GCM - android

let me explain my requirement, I want to send a notification to my users on their(Android) when any changes happen on table.
Using SAP NW Gateway I could able to send notification to any listener(listener class)when my table record updated/deleted.
Now I want to send this notification to users android devices,I have seen GCM but not able to understand how can I post my notification received to GCM and send that message from GCM to Android devices.
Please let me know if I can post my notification details to GCM how to send(read the sent message over GCM) to devices.
Notifications(using SAP Gateway) I am receiving is in XML format.
Thanks
Rajesh

To send a message, the application server issues a POST request to https://android.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
Contents of data lands on connected GCM clients. Of course, you must first configure GCM API in your google developer's console.
https://developer.android.com/google/gcm/server.html
https://developer.android.com/google/gcm/http.html

Related

How do I receive push notifications of new Outlook emails to an Android device?

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.

sending a notification to other users when a user clicks a button using firebase

I am using Firebase for my android studio project and I want to have a notification button so that whenever a user clicks a send button, the notification button indicates there is a new notification and that notification contains the data that the user sent. Is it possible to do so using firebase ? and how ?
Sure. You want to use Topic Messaging in FCM.
Here's the setup guide.
Once implemented, subscribe a user with:
FirebaseMessaging.getInstance().subscribeToTopic("dogs")
And send a notification via a POST request on the app (insecure, not recommended) or via a server (recommended):
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
I don't know, whether you figured it out or not (because you asked the question months ago), but I may have an idea. Actually I was also searching for what you asked to do, and I read your question. I wanted to do the same thing as you. Then I thought, if firebase cloud messaging service can receive http POST request not just from firebase console but also from any other service like ARC (Advanced REST client).
So the solution :
Send a JSON HTTP post from your app when the users click on the app with the required data received from user. Now when the JSON response is received by the firebase cloud messaging server, it should you the notification to every user with the data sent by the user who clicked the button.
I haven't tried it yet but I am going to try it now. I will let you know in the comments if it works or not. It should work theoretically. If you have already figured out a way to send such notifications, please don't forget to share your method.
If you want to know how to send JSON HTTP post, then here is a stack overflow answer : SENDING JSON OBJECT

Android FCM Push notification is NOT delivered

I sent the following notification json request to FCM gateway:
https://fcm.googleapis.com/fcm/send
And the following is the json request:
{ "data": {"body": "Nice to meet you!"},"to" : "dWgg2uGMlrs:APA91bFWhoMvV2WIZrYlENUqHzP0J2fXTuBGo-FiFd-YwGUT6vqyTjeGiOi28rOnU6MQggDwziQ7Xwg4mw6Fbnjo4-OqfOKsfw1M4E6w2rRxc0yQyGbKhQEGpIGC2eIc2CACYrEudsxz","priority" : "high"}
And the following is FCM gateway response
{"multicast_id":7757558437981419128,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1489893859089718%f58ec05df9fd7ecd"}]}
However, my my phone is not able to receive the notification. If I use the exact same device token dWgg2uGMlrs:APA91bFWhoMvV2WIZrYlENUqHzP0J2fXTuBGo-FiFd-YwGUT6vqyTjeGiOi28rOnU6MQggDwziQ7Xwg4mw6Fbnjo4-OqfOKsfw1M4E6w2rRxc0yQyGbKhQEGpIGC2eIc2CACYrEudsxz and send notification in FCM web console https://console.firebase.google.com
I am able to receive the notification.
Why?
Never mind. The request and response are totally valid. There was a bug in my android app. Issue closed.

Unable to send data message using firebase console

I'm using firebase console and can send only Notification messages using it.
Is there a way to send data messages using the same?
The Firebase Notifications Console can only be used to send notification messages. It cannot be used to send data messages.
See the table in message types in the Firebase documentation:
Notification message
Use scenario: FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys.
How to send:
Use your app server and FCM server API: Set the notification key. May have optional data payload. Always collapsible.
Use the Notifications console: Enter the Message Text, Title, etc., and send. Add optional data payload by providing Custom data in the Notifications console. Always collapsible.
Data message
Use scenario: Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
How to send:
Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.
You can test both notification message and data message using Postman(rest client for testing http request).See screen shots:
In header pass:
key:Content-Type, value:application/json
key:Authorization:key=<Server key>
Please look here: Firebase push notifications update DB, my post from June.
In conclusion, you need send HTTP POST request to https://fcm.googleapis.com/fcm/send
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
You can now send notification message via the console. Note that it is different from data messages; notification messages only trigger the onMessageReceived callback when the app is in the foreground.
They are inside the advanced options tab on the compose message screen.
Just expand it and type your key/value map.
These will be included into the data field of the notification.

Getting Bad Request (400) on Azure Notification Hub's "Test Send"

I'm trying to send the following payload to a specific device (which is why I'm using the registration_ids) via Test Send on Azure's Notification Hub, but I'm getting a bad request error.
{
"registration_ids" : ["2670873167838193734-9582072870486547213-1"],
"data":{"message":"Hello World!"},
}
I'm not sure why since I checked the formatting and it looked fine to me based on other sources I saw.
Does anyone have an idea on what's happening?
EDIT:
Here's how I eventually managed to send a notification to a specific user:
on the client side (Java), I replaced the line
hub.register(regid)
with
hub.register(regid,"myTag")
Where "myTag" can be any other string tag you want. That way you can enter the word myTag in the 'Send to Tag' on Azure's Notification Hub and verify that you received the notification on your device.
Also, if you want to send the notification from the server side (C#), you'll need to add this header to your http request:
request.Headers.Add("ServiceBusNotification-Tags", "myTag");
Hope this helps anyone.
registration_id would not be part of the payload (see https://msdn.microsoft.com/en-us/library/azure/dn223273.aspx request body) although it is part of GCM's payload. To send to a specific device, you will need to tag that device with an id of sorts (e.g. registration_ids) and send to the tag "{registration_id}".
This is due to registration_ids field in the payload. Notification Hub does allow passing the registration_ids in the payload. Notification Hub internally substitute the registration id (before sending to GCM/FCM) based on selected devices.
https://msdn.microsoft.com/en-us/library/azure/dn530749.aspx
Thanks,
Sateesh

Categories

Resources