I have made the Firebase Cloud Message Notification working even work through the Notification Hub from Azure. The time I can't receive the message is when I try to re-run the application.
Process:
1) Fresh install the application with Visual Studio IDE
2) Stop the debugger
3) Debug and run the application again through Visual Studio IDE
4) Send a test message through FCM Console
If I am not doing 3, I still can receive the message even if the application is in background
LoadApplication(new App());
FirebaseOptions options = new FirebaseOptions.Builder()
.SetApplicationId(GetString(Resource.String.fcmAppId))
.SetApiKey(GetString(Resource.String.fcmApiKey))
.SetGcmSenderId(GetString(Resource.String.fcmGCMSenderId))
.Build();
FirebaseApp.InitializeApp(Android.App.Application.Context, options);
?
public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseInstanceIdService";
public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Settings.NotificationToken = refreshedToken;
Android.Util.Log.Debug(TAG, "Refreshed token: " + refreshedToken);
SendRegistrationToServer(refreshedToken);
}
void SendRegistrationToServer(string token)
{
}
}
Initially I thought it has a different token but is the same.
Message from Azure Portal:
The token obtained from the token provider is wrong.
Android using Firebase Cloud Messaging not receiving message
As the GCM docs said :
An ID issued by the GCM connection servers to the client app that allows it to receive messages.
So only when the token is available, your app can receive message from GCM.
How can an app get that token?
You could see the document :
On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token.
The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh
Every time when you re-run the application in your device the token is changed, but as the document said, OnTokenRefresh is only called when the system determines that the tokens need to be refreshed, it is needed for key rotation and to handle Instance ID changes due to :
When the app is installed or uninstalled.
When the user deletes app data.
When the app erases the Instance ID.
When the security of the token has been compromised.
You need to trigger OnTokenRefresh method. You should first uninstall the app from the device, then reinstall the app and open it, the OnTokenRefresh will be triggered and the token will be updated and your app could receive GCM message again.
Related
I would like to get the registrationID from my app. Does the registrationid exist before your app gets sent to the store ? If it doesn't how to I test push notifications ?
The device registration token that FCM uses to deliver messages for your app to a device is created when an app with the Firebase Cloud Messaging SDK is installed on that device. Your app does not need to be in the Play store for that to happen. See the documentation on accessing the registration token to learn how to get that token in your application code.
The easiest way to test delivery is to send a notification through the Firebase console, in which case you only need the FCM registration token or a topic (that your application code is subscribing to).
To send a message to the device from (server-side) code, you need the FCM token (or topic again), the FCM server key (or OAuth authorization), and the sender ID.
I am trying to build a wrapper app with cordova and using PHP as server backend.
I am using cordova-plugin-fcm to handle push notification.
Correct me if I am wrong, each device (android and ios) has own id which is used to send notification.
How can I get that id and send it to PHP route so that I can bind it with the logged in user and send notification?
//FCMPlugin.onTokenRefresh( onTokenRefreshCallback(token) );
//Note that this callback will be fired everytime a new token is generated, including the first time.
FCMPlugin.onTokenRefresh(function(token){
alert( token );
});
In above example token is the id to be send to PHP to send notification?
Can the device.uuid used for sending notification?
I have already setup my Firebase configuration and the project has google-services.json and GoogleService-Info.plist in place.
Thank you
Firebase Cloud Messaging targets its messages at a specific app on a specific device. Each specific app install is identified by Firebase's Instance ID, also referred to as a device registration token, or an FCM token.
The Firebase documentation on accessing the registration token says this about it:
On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseMessagingService and overriding onNewToken.
From a quick read through the Cordova documentation on device.uuid, this seems to merely identify the device, and not the app on the device. Since FCM messages are delivered to a specific app on each device, it seems unlikely you can use the UUID of the device as a replacement.
Even if the UUID is unique for each app on each device, it won't be a drop-in replacement, as FCM only works with its own registration tokens. At the very least you'll need to keep a mapping of the device.uuid values to their corresponding FCM token.
Am setting up push notifications for my Android App. The App is not published into Google Play Store yet. Am testing this app by installing APK directly on my mobile.
Can FCM (Firebase Cloud Messaging Server) send notifications to mobile phones which has done installation from APK files directly (not through playstore)?
Just want to know how FCM will work in this case?
YES. FCM can send notifications to your app as long as your device is registered to receive messages with FCM server
On initial startup of your app, the FCM SDK generates a registration token for the client app instance. FCM Server will manage the registration token and use this token to identify your device and send notifications. Note that the registration token can be changed in some case View more here
You can test by sending notifications to your device from Notification Composer. Select target is Single Device and your registration token is target device.
// Get the current registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
To handle message follow the document
Yes you can do by using Firebase console
In firebase console select Function tab which is present in left part of firebase console.
Download node.js in your system and code it according to your wish in node.js.
Firebase token should be mention in your java code.
in `Firebase Cloud Messaging (FCM) I m getting device token.and notification is coming .but in my app there is two user so i m sending notification for 2 user notification is coming.But i m logged in as 1 user. because both device id stored in my database and based on device id is fire base sent notification.so i want to restrict or unregistered fire base device token .if i m not logged in as 2user.i can delete that device id from database that is one solution. but some reason i cant delete for counting user it is required .so help me for this situation..thanks for helping
FCM is running on GCM as its core. The behavior related to GCM when an app is being uninstalled should still be the same for FCM.
When your app is uninstalled, it is the developer's responsibility to unregister/remove the corresponding registration token from your own App Server, along with the other actions (in your scenario, count the number of uninstalls) you need.
In turn, the FCM server will determine if the app is uninstalled and then invalidate the corresponding registration token. From the docs:
Finally, when FCM attempts to deliver a message to the device and the app was uninstalled, FCM discards that message right away and invalidates the registration token. Future attempts to send a message to that device results in a NotRegistered error.
I created a Push Notification Server (PNS) by using Google Cloud Messaging (GCM).
With this architecture, my server has to know the token associated to a user in order to send him push notifications.
I extended the class InstanceIDListenerService which is correctly notified when a token has been refreshed (I tested it with adb). When its method onTokenRefresh() is called I take the new token and, so far, all is ok.
My problem is the following: what if I fail to send this new token to my server? (a network error, a lack of connection, a sudden device shutdown, or something else...).
Do I have to store somewhere (preferences?) that server does not know my new token and retry when possible? Is there a way to let the OS to perform this operation?
You may use GCM NetworkManager to queue, schedule, check network connections and... , its very reliable and useful, it handles all of your desired tasks.
Also you have to save your token some where is your device, may be on your shared preferences, till you make sure your server recives it. Please note that if you want to use other features of GCM like topic messaging, you have to save and use token for all topic registrations.
Your app (when it runs) should always check if the token has been sent to the server. When onTokenRefresh is called you should generate and send a new token to the server. If sending the token to the server fails then the next time your app runs it should try again.
You are correct that if you fail to send the token to the server onTokenRefresh your app will not receive notifications till it is run again and successfully sends the token to the server.
Also note that token refresh should not be a regular occurrence. It should only happen for example when the app is uninstalled and reinstalled or the token is deemed to be no longer secure.