Firebase Android Client to Client Notification Message - android

I would like to send a notification message from client A to client B. I understand that this is possible with the Firebase Android SDK by using the FirebaseMessaging class and the RemoteMessage.Builder. I assume this works fine if both clients are in the foreground. But if one client is in the background this won´t work because with RemoteMessage.Builder you can only create data messages with the methods setData and addData but no notification messages which we need in case one client is in the background. Why is this not possible directly with the SDK?

I would use onesignal instead of Firebase , also consider that firebase doesn't supports device to device notification push.... it would be easier i guess..

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.

How to sent push Notification to all users from firebase when new data add on Database

I am trying to trigger database by node.js . But i did not found any solution for this problem. Once I heard to store the device token for the sending notification.But it was for the device to device sending notification.If I sent notification for all users then should i store the device token for all users??
database tree in firebase
You can send push notification to all the user without saving device token.
For this you need to add following line in onReceive method:
FirebaseMessaging.getInstance().subscribeToTopic('news');
Then you can send push notification to that topic, it will be sent to all the user who are registered with that topic.
Note : Similarly the client can unsubscribe from a topic by calling unsubscribeFromTopic() method.
FirebaseMessaging.getInstance().unsubscribeFromTopic('news');

How to send message from Firebase to device when app is killed? [duplicate]

This question already has answers here:
Android app not receiving Firebase Notification when app is stopped from multi-task tray
(6 answers)
Closed 6 years ago.
I am trying to get familiar with Firebase Notifications. It works fine, but I am stuck with receiving messages from the notification console when the app is not turned on.
I know that documentation says that:
if your app in foreground or background you can receive message in onMessageReceived method, otherwise user will receive notification in tray... click on it will open main activity with data inside intent
But is there are any way to catch every message from the notification console even if the application is closed?
==== ANSWER ====
Find answer here
There is no way to send data message from the notification console.
But there are other ways to send notification to devices and they will be caught inside onMessageReceived!
You can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
Authorization - key=your_server_key_here
Content-Type - application/json
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
onMessageReceived() method will not be called if the app is in background or killed only when the message is sent through Firebase Console.
When app is not running you will anyway receive notification from firebase console. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only data payload to fcm endpoint. In short you will have to create an app server if you want to actually utilize FCM more efficiently in this case .
You can have a look at these two questions which discuss more regarding same and in second question I also discuss briefly over using node.js and java servlet to do the same:
Handle the data payload without user tapping on the notification?
How to push notification to client when Firebase has a new entry?
Do let me know if this provides you with some info.

Mobilefirst PushNotification: On using wl_anonymousUserRealm at server, app is not calling onReceive method on clicking notification

I am working on IBM Mobilefirst native android app. I have written code to enable push notification. I am getting notification but I have an issue here.
On Launch of the app I am calling following code.
final WLClient client = WLClient.getInstance();
push = client.getPush();
ResponseListener listener = new ResponseListener(ResponseListener.AUTHENTICITY_CONNECT);
client.getPush().setOnReadyToSubscribeListener(listener);
challengeHandler = new AndroidChallengeHandler(realm);
client.registerChallengeHandler(challengeHandler);
WLRequestOptions options=new WLRequestOptions();
options.setAppUserId("sample");
client.connect(listener,options);
When I launch the app for the first time all the above code gets executed and the listener calls the below method
#Override
public void onReadyToSubscribe() {
WLClient.getInstance().getPush().registerEventSourceCallback(pushAliasName, "PushAdapter","PushEventSource", this);
}
After this listener is getting executed i am calling subscribe method. I get success for the subscription to push.
On the server side I call the procedure to send the push notification and it reaches the phone.
Now when my app goes to background and I get a notification . I click on the notification and app restarts and never call onRecieve method of the registered interface.
On click of the notification it relaunches the app and call the onReadyToSubscribe() again and it never calls the onRecieve method. what should I do to get the onReceive() method to be called and app shouldn't get relaunched(if app is already in background) on click of notification?
My server side security test is as below
<customSecurityTest name="AuthSecurityTest">
<test realm="wl_antiXSRFRealm" step="1"/>
<test realm="wl_authenticityRealm" step="1"/>
<test realm="wl_remoteDisableRealm" step="1"/>
<test realm="wl_anonymousUserRealm" isInternalUserID="true" step="1"/>
<test realm="wl_deviceAutoProvisioningRealm" step="2" isInternalDeviceID="true"/>
</customSecurityTest>
The useridentity is not binded to security test but i am putting it in the apps applicationdescriptor. So it never call for credentials required of challenge handler on connect.
Here I think this could be the issue in the sample code that is provided by MFP in 7.1 version security test has useridentity realm but in my case I am not using the custom useridentity realm but I am using the default wl_anonymousUserRealm. This is the issue because when i tried working with the sample code it works completely fine with all the scenarios. But with the wl_anonymousUserRealm I have this issue.
onReadyToSubscribe() method getting called everytime the application connects to the server ( even if already subscribed for push) is expected and by design.
onReadyToSubscribe() is a call back that fires at the client denoting the token exchange between client and server is complete. This is required because tokens issued to an application by mediator can change. This makes it imperative that the server always stays updated with the latest token. If not , push notification will fail with invalid token error.
When the client connects to the server , they compare tokens - client presents the token it has now and server presents what it had stored. Three cases happen:
1) In a new registration , server does not have a token. Here client passes on what it got from the mediator, server persists it and issues a success. Client fires the onReadyToSubscribe() callback to indicate push handshake is complete and client can now subscribe to aliases or tags.
2) Client has received a new token from the mediator. This does not match the one server already has. Client passes on the new token to server, server updates its records with the new token and lets the client know. onReadyToSubscribe() call back fires at client indicating successful handshake. Earlier subscriptions records stay.
3)If the tokens match, then onReadyToSubscribe() fires denoting that push handshake is complete (no changes required)
Thanks to Vivin for the suggestions.
Here was the issue in the sample code that is provided by MFP in 7.1 version security test has useridentity realm but in my case I am not using the custom useridentity realm but I am using the default wl_anonymousUserRealm.
The actual issue was wl_anonymousUserRealm generates a new random userid everytime. So when ever my app is relaunched my userid gets changed and I wont get push in onrecieve because it thinks a different user is logged into the app..
Now because I cant change the wl_anonymousUserRealm I am sending push using deviceid. Please check the below link(Adapter code as below).
https://www.ibm.com/support/knowledgecenter/SSHS8R_7.1.0/com.ibm.worklight.apiref.doc/html/refjavascript-server/html/WL.Server.html#sendMessage
check sample client code from below link.
https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/7.1/notifications/push-notifications-overview/push-notifications-native-android-applications/tag-based-notifications-in-native-android-applications/

Android PushNotification From Json URL

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.

Categories

Resources