I tried a sample code to test the push notification system with Firebase and it's working well except one thing.
If I try to send a notification from Firebase console, using Device Token, the notification shows in device.
If I try to send a notification from Firebase console, using my topic topik, all notifications show in all devices.
If I try to send a notification from my web page or from postman, using Device Token, the notification shows in device.
If I try to send a notification from my web page or from postman, using my topic topik, NOTHING HAPPENS.
This is a example call:
link: https://fcm.googleapis.com/fcm/send
POST method
Header field:
Content-Type : application/json
Authorization : key=MY_SERVER_KEY (the new one)
Body:
{
"to": "/topics/topik",
"data": {
"title": "This is a Firebase Cloud Messaging Topic Message!",
"content-text": "This is a Firebase Cloud Messaging Topic Message!"
}
}
or
Body:
{
"to": "/topics/topik",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
}
}
the result on send action is something like this
{
"message_id": 7150560334538835864 (SUCCESS!)
}
but no notification arrives in any device. I tried to debug the onReceive method, but nothing happens.
Any idea?
Are you trying to send data-messages or notification-messages?
see: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
If you want to send notification-messages (the type of messages sent by the notification console)
the payload you wrote in the question is wrong. Try replacing data with notification:
Body:
{
"to": "/topics/topik",
"notification": {
"title": "Hello",
"body": "This is a Firebase Cloud Messaging Topic Message!"
}
}
Related
I am creating a college Android application where faculty choose the specific course and send them assignment notification but how i do that i don't Know... please help me..
You need to subscribe topic from Android app. Send notifications to that topic so it will received to all users who has subscribed it.
Sample code subscribe top from Android app.
FirebaseMessaging.getInstance().subscribeToTopic("news");
Send message to this topic from server-
URL- https://fcm.googleapis.com/fcm/send
Body-
{
"to": "/topics/news",
"data": {
"message": "Message to user",
"title":"title of message",
"sender":"sender name"
}
}
headers- api key from firebase
Rest you need write code from Android and Server side.
You can also send notifications form Firebase console as well.
I figured how to send and receive Firebase Push notifications using Firebase Cloud Messaging
I want to know how to send a notification manually using code in android studio so I can send it from any phone.
You send messages from your phone using FCM. You need to make a POST to https://fcm.googleapis.com/fcm/send api with payload that you want to send, and you server key found in Firebase Console project.
As an exameple of payload sending to a single user with to param:
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
Also, to param can be used for topics "to": "topics/yourTopic"
In data you can send whatever you want, message is received in onMessageReceived() service from Firebase.
More details you can found in Firebase documentation.
Im implented this GCM example into my Android application. When I trigger a message it will be received by all devices, which have installed my app.
Is it possible to address a single device by purpose (also with other libraries/technologies other than GCM)?
Is it possible to address a single device by purpose?
Yes, of course, it is possible.
There are two ways.
1. Using GCM Registration token :
You can target a single device using it's GCM registration token as mentioned
here.
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
2. Using GCM Topics :
GCM topic messaging allows your app server to send a message to
multiple devices that have opted into a particular topic as mentioned
here.
Client Side :
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
pubSub.subscribe(token, "/topics/foo-bar", null);
}
Server Side :
{
"data": {
"message": "This is a GCM Topic Message!",
},
"to": "/topics/foo-bar"
}
For both the ways,
Add your target device's registration token string or topic name under to section of a message request.
Hope it helps. :)
As documentation says, I can send message with topic as
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
}
But to send message to specified device, i need to set to as device's push token as "to":"cvE0coiuUEg:APA91...". But in this case, how should I set topic?
They are different types of messaging. You want to use Device Group Messaging. In that case, you do not need to set a topic.
We are developing a cordova based android application which has chat capability.
We are sending test chat messages from a server and we received the messages just fine when the app is in foreground.
We would like to get a notification about chat message when app is in background.
However we are not getting any Push Notification about chat message.
The steps we followed are mentioned below. Kindly let us know the mistake we're making.
We have referred this link to get notification on device when app is in background.
We use cordova PushPlugin to register device for push notification. And register device using below code
pubnub.mobile_gw_provision ({
device_id: 'A655FBA9931AB',
op: 'add',
gw_type: 'gcm', // or 'apns'
channel: 'my_chat',
callback: mySuccessCallback,
error: myErrorCallback,
});
We get the Push Notification if we programmatically send use the GCM device id to send Push notification from our server using GCM API.
We receive notification alert from pubnub in onNotificationGCM method of PushPlugin when application is in foreground.
We have also configured GCM server API key in admin console and enabled pubnub push notification.
I am using below Python code to publish the Chat:
_pubnub = Pubnub(publish_key='our-publish-key',
subscribe_key='our-sub-key')
channel_name = 'here-is-channel-string'
data=dict(
pn_gcm=dict(data=dict(
title_for_mobile='PN',
summary_for_mobile=['Hi, test']
)
),
text='what is your dob?',
sender=dict(name='Jon Snow', id='yyyyyyyy'),
meta=dict(job=dict(id='zzzzzzzz'))
)
_pubnub.publish(channel_name, data)
Getting push notification of incoming chat message when application is
in background is a key functionality of my application.
Please let me know what I am doing wrong?
I had also raised a support request regarding this on Pubnub's support. After some nice replies from guys at Pubnub, we were able to resolve it.
We were mislead by this documentation, and we were using
"pn_gcm": {
"data": {
"title_for_mobile": "Test",
"summary_for_mobile": [ "Hi" ]
}
}
Instead, we should have used.
{
"message": "This is some text",
"pn_gcm": {
"data": {
"title": "Demo wpush",
"message":"This is a pushnotification"
}
},
"pn_debug": "true"
}
Because Cordova PushPlugin is expecting message key from GCM.
// GCMIntentService.java
#Override
protected void onMessage(Context context, Intent intent) {
//.... some code ...
if (extras.getString("message") != null && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
//... some code ...