I am using firebase to send notification (using token) to both android and IOS and applications are working fine. Now my requirement is:
When apps receive a notification, that time notification is also received in web application.
How it is possible, suggest me.
Thanks.
I am using code by using link:
https://rakibul.net/fcm-web-js
but this is work for browser notification.
Sending is the same.
If you have some account for each user, you can group them under the device groups and then send it to all of them at one time.
Frontend is same as in your link.
By https://firebase.google.com/docs/cloud-messaging/android/device-group
create device group
https://fcm.googleapis.com/fcm/notification
Content-Type:application/json
Authorization:key=API_KEY
project_id:SENDER_ID
{
"operation": "create",
"notification_key_name": "appUser-Chris",
"registration_ids": ["4", "8", "15", "16", "23", "42"]
}
This returns you notification_key, which you can save to your users account.
If you want to add another device for the user, call
{
"operation": "add",
"notification_key_name": "appUser-Chris",
"notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ",
"registration_ids": ["51"]
}
And you can then send your message to the notification_key insted of registration_id.
Related
Hello I'm using Flutter to build my app and I need to to show an alert whenever a new notification is received.
I've been using firebase_messaging 7.0.3 but I run into an error with onBackgroundMessage. A quick Google search helped me find out that the error I was getting hasn't been fixed yet. However one of the devs posted an update 20 days ago about a new version of the package which fixed that issue.
The new version removed the old onMessage handlers and introduced new ones.
Now they got new event handlers which return streams, but haven't been able to make them fire by using the
.listen() function. Whenever I receive a notification a get a this: D/FLTFireMsgReceiver(22032): broadcast received for message printed in the console but the code in the .listen() doesn't get executed.
Here is a link to an article on Firebase Flutter that is a guide for using the new version of the package.
Here is my code:
...
FirebaseMessaging.onMessage.listen((event) {
// do something
});
FirebaseMessaging.onMessageOpenedApp.listen((event) {
// do something
});
FirebaseMessaging.onBackgroundMessage((message) {
// do something
return;
}
...
A solution I found to get the events to fire was to always call:
await FirebaseMessaging.instance.getToken();
right after the
await Firebase.initializeApp();
Once I call that, the FirebaseMessaging.onMessage.listen catches the event as expected.
I was getting the same log when my app is in doze mode for the Data notification with high priority.
This is because of some issue in the firebase-messaging plugin.
Firebase_messaging plugin internally uses JobIntentService to process background fcm notifications
JobIntentService has one constraint in Android O or later versions, when running a Job it will be subject to standard JobScheduler policies. The job will not run immediately while the device is in doze mode. (reference link)
The same issue was raised in the firebase_messaging git repository(bug link)
Solution
One Signal(another push notification provider) solved this issue by having a modified version of JobIntentService. (OneSignal Solution)
At a high level, it uses wake locks for high priority fcm notifications to run service even in Android O and above.
Add this Pull Request changes in your ide by editing respective files.
TL;DR
Add this Pull Request changes in your ide by editing respective files.
Send Data notification with High priority.
FCM Payload:
{
"message": {
"token": "fcm_client_token",
"data": {
"title": "Hello",
"body": "Test Message"
},
"android": {
"priority": "high"
}
}
}
I'm sending to Firebase Cloud Messaging with the following:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=[SERVER_KEY] (The one found in Firebase Project>Settings>Cloud Messaging
Body:
{
"notification" = {
"title": "Title",
"text": "Your Text",
"sound": "default",
"badge": "1"
},
"to" = "a4fbc860-23fd-4ffc-95b7-11c90d8db320" (The Device ID from the hand held)
}
When I send this I continually receive: {"results":[{"error":"InvalidRegistration"}]}
I'm sure it's something simple that I'm missing. The docs aren't very clear on what elements are required and the nomenclature is different between the error messages and the Firebase site. I've tried to swap out "registration_ids" with "to" (using the array, of course), but the result is the same.
When sending to a single device, "to" field should specify an FCM registration token, not a Device ID. You can retrieve the registration token using FirebaseInstanceId.getInstance().getInstanceId() method.
This token looks like two strings, a short and a long one, separated by a colon, e.g. "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
Links to relevant documentation:
Registration token
Legacy HTTP Protocol errors
Could I send message to Mozilla push service endpoint with group ids, which like Device Group Messaging of FCM ?
/*
some code like :
"registration_ids": ["4", "8", "15", "16", "23", "42"]
*/
Mozilla autopush is a standard push service and currently the W3C Push API doesn't support groups or segments.
The only options are:
manage segments by yourself
use a service which adds an additional layer of abstraction and supports groups and tag-based routing
I am using the latest Bluemix Push Notification service. I'm using the MFPPush API to register the device (Android).
Here is my code snippet:
var success = function(message)
{
console.log("Success: " + message);
alert("Reg Device: " + message);
};
var failure = function(message)
{
console.log("Error: " + message);
alert("Error: " + message);
};
MFPPush.registerDevice({}, success, failure);
var notification = function(notification)
{
// notification is a JSON object.
alert(notification.message);
};
MFPPush.registerNotificationsCallback(notification);
The success message contains the following information:
Token:APA91bFtkSr59Zxlr52HU****Uij
UserId: ""
DeviceId: g5c6d98f-0867-3fd1-a353-15bcdef675a2
When I send the notification, my device receives the message.
The Swagger REST API shows that I can arbitrarily give some token, userId and deviceId:
{
"deviceId": "TestDeviceId",
"platform": "G",
"token": "************",
"userId": "Joe"
}
How do I get the "TestDeviceId", and how do I get the "token"? I don't see any API to get that information.
Note: You should probably just use the Client SDK to register as it handles all of this in the background--automatically assigning each device a unique ID. You shouldn't explore this unless you know what you're doing. There isn't a really a reason for trying to manually set the deviceID.
When you register a device for the Push Notifications service, you set these values in the body of the POST request. On a successful call, it will return these values in the response. I'll do a demo of this later in the post.
You can also retrieve a list of the device registrations for the Push Notifications service.
You can use that deviceId to retrieved detailed information about the device, send a specific push notification to that device, subscribe it to a tag and send push notifications to those devices, etc.
Swagger Documentation is here.
Regarding those values, you can put whatever you want in them when you register. Typically, these values would be set automatically by the Bluemix Mobile Android/iOS Client SDK when you do the register call. However, you could do this manually using the REST client.
For example:
Here, I'm registering a device:
It registered successfully:
This is what I get if I ask the Push Notifications service for information about my registered devices (for the "deviceId": "arandomdeviceid"):
The Android BMS Core Client SDK sets this deviceId using a unique UUID from the device and hashing it with MD5.
You can look in here more more information about that.
#anamica (a) Allow userId parameter to be passed along with the MFPPush registration like MFPPush.register({"userId": "AUniqueUserId"}, success, failure) (b) Add an additional parameter to the target "userIds" (array).
This enhancement has been done, you can give a try by updating the latest SDK.
I am trying to use the messages service in the Bluemix Services Push REST API to send a push notification to users who are subscribed to previously created tags. The problem is that when I send a notification to multiple tags, if a user is registered to more than one of those tags he is receiving the same notification multiple times.
The API says this:
tagNames (Array[string], optional): Send notification to the devices that have subscribed to any of these tags
So I understand that the user should only receive one notification, instead of what is happening.
My call looks like this:
{
"message": {
"alert": "Text notification",
},
"target": {
"tagNames": ["TAG1","TAG2","TAG3","TAG4","TAG5","TAG6","TAG7"]
},
"settings": {
"apns": {
"badge": 1,
"sound": "UILocalNotificationDefaultSoundName"
}
}
}
Confirmed with the Bluemix Push development team that this is indeed a defect in the service and you should only be receiving one notification in this instance. The team has created a defect for the issue and once it has been resolved and pushed to production I'll update this answer with whatever additional information or changes in usage come from the fix.
Thank you very much for bringing this to our attention