Reputation: 752
I am trying to get notification generated by FCM console and I am receiving them but I am unable to override onMessageReceived
of FirebaseMessagingService. Don't know what I am doing wrong.
MyFirebaseMessagingService class responsible for handling notifications:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "FROM:" + remoteMessage.getFrom());
//Check if the message contains data
if(remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data: " + remoteMessage.getData());
}
//Check if the message contains notification
if(remoteMessage.getNotification() != null) {
Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData());
}
}
/**
* Dispay the notification
* @param body
*/
private void sendNotification(String body , Map<String,String> data) {
// int finalSecId = Integer.parseInt((String) data.get("sec_id"));
// int sec = Integer.parseInt((String) data.get("sec"));
Intent intent = new Intent(this, InsuranceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
//Set sound of notification
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.login_meter)
.setContentTitle(getString(R.string.app_name))
.setContentText((String) data.get("sec_id")+ " "+(String) data.get("sec"))
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
}
}
And Inside Application tag
<service android:name=".Fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".Fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Upvotes: 1
Views: 3033
Reputation: 4248
Extending #Sudip Podder comments and #Ratilal Chopda answer Follow these steps:
Step1:
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name=".SplashActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Step2:
I am using php at server so you need to adjust things the way you like but in notification payload add "click_action" : ".SplashActivity"
$fields = array(
'to' => $token,
'notification' => array(
'title' => 'Motors City',
'body' => $message,
"click_action" => ".AppSplash",
),
'data' => array(
'sec_id' => $secID,
'sec' => $sec,
'extra1'=>$extra1,
'extra2'=>$extra2
)
);
$headers = array(
'Authorization:key=' . $server_key,
'Content-Type:application/json'
);
Step3:
In Oncreate of your SplashActivity
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Log.d(TAG,bundle.toString);
}}
and you are done
Upvotes: 1
Reputation: 2170
I had this problem of handling the notifications from FCM.
First we have to understand that there are 2 types of notifications.
Notification - It will trigger when your app is not in foreground and generate a notification. If you click on it then it will open the launcher activity.
Data notification - This one is used to parse the data and it is received in background as well as foreground. So you can build a custom notification based on the data provided in the data object by the FCM Push.
Map<String ,String> dataMap = remoteMessage.getData();
Here i created a simple Map with key value pairs. Now i can receive the title of the notification in the data object and make a simple notification with a custom intent.
I personally use a context object to determine if the app is in foreground or background. Based on that i decide if i have to show the notification or just update the data.
Hope this helps.
Upvotes: 1
Reputation: 357
Right now you are having Notification in notification type, which triggers Notification Default,and Just open the app on the click of the notification.
So you need to change server side code from notification type to data type. And try to get Message from
`remoteMessage.getData()` not from `remoteMessage.getNotification()`
if you want to manage click of the notification use data type notification.to understand more about this types go through this link https://firebase.google.com/docs/cloud-messaging/concept-options
Upvotes: 0
Reputation: 1020
Try this
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
Upvotes: -1
Reputation: 4220
There are two types of FCM
notification Messages: Sending a payload with this message type triggers onMessageReceived() only when your app is in foreground.
data Messages: Sending a payload with only this specific message type triggers onMessageReceived() regardless if your app is in foreground/background.
Reference:here
Upvotes: 2