I am trying to open an Activity
when the notification is clicked and below is my code.
Intent intent = new Intent(this.getApplicationContext(), NotificationActivity.class);
intent.putExtra("msgBody",messageBody);
intent.putExtra(Constants.NOTIF_INTENT_TYPE,Constants.NOTIF_INTENT_TYPE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|Intent.FLAG_ACTIVITY_SINGLE_TOP
|Intent.FLAG_ACTIVITY_CLEAR_TOP); //Tried with many options here
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.otp_icon)
.setContentTitle("Push MSG")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.com.pushapp">
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".AndroidPushApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher">
<activity
android:name=".PushSplashScreen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainApplicationScreen"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<activity
android:name=".StartActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:uiOptions="splitActionBarWhenNarrow"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".NotificationActivity"
android:exported="true"
android:label="@string/title_activity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Whenever I get notification from FCM I am calling this notification. The NotificationActivity is not opening whenever I click on notification, rather the app is opening(splash screen->starting activity
of my usual app flow). Whenever I get notification while the app is already open, the NotificationActivity
is getting opened, but not when app is not already opened. Could someone please help me on resolving this?
Note: Please I am reiterating that NotificationActivity.class
is not getting opened when clicked on notification when app is not already opened state.
Thats the intented behaviour. If your app is in background, notification is created by android system which does not have your pendingIntent action.So it does not work. In the foreground case it works because notification is created by your code.
Please check the doc in the below link. https://firebase.google.com/docs/notifications/android/console-device#receive_and_handle_messages
According to
FCM
Documentation, for receiving and handling messages,So Basically, we have two types of Payloads
1). Notification Payload
2). Data Payload
3). Both (an additional type we can consider).
Now let's discuss one by one these payloads. Before that you need to understand how can you send these Payloads to your app. All you have to do is to make use of any tool that can perform
HTTP POST Request
. In my case, I am using the Postman tool, a Google Chrome Plugin.Before making a
HTTP Post Request
forFCM
, you have to consider three things:1). HTTP Post Request URL : https://fcm.googleapis.com/fcm/send
2). Request Headers :
i). Content-Type : application/json
ii). Authorization : key = YOUR_SERVER_KEY
Below is the screenshot for the same to show how it looks.
3). Body : In this we are going to have
JSON
forNotification
andData Payloads
.onMessageReceived()
is called only when the app is inForeground
, For all other cases, it's aSystem Tray Notification
, which opens theLauncher Activity
when clicked. This is helpful when you don't want to controlNotifications
by your own and not much data to deal with whenNotification
comes. You can even control the sound, icon and click_action(only when the app is inForeground
) without writing any code in youronMessageReceived()
. One example of a body of suchHTTP POST Request
is attached in the screenshot below.For opening desired
Activity
when sending click_action parameter, you have to use the below code in youronMessageReceived()
.and below is your
startActivity()
method :GCM
. This is very important if we want to handle all theNotification
stuff by ourselve same as we all were doing in case ofGCM
. Example of a body of suchHTTP POST Request
is shown below.So in this case,
onMessageReceived()
is called everytime and this will work in the same way as that ofGCM
, so helpful to all of us. You have toOverride
onMessageReceived()
as shown below.Notification
andData Payloads
as well. In this case,onMessageReceived()
is called when the app is inForeground
. For background and closed state,Notification
comes in the system tray similar toNotification Payload
but the only difference is we can havedata extras
as well that we can use to redirect user to a desiredActivity
, when clicked on aNotification
. Below is the example of a body of suchHTTP POST Request
.Example of a body of suchHTTP POST Request
is shown below.When clicking on a
Notification
on System Tray, it will open theLauncher Activity
and You need toOverride
onCreate()
of yourLauncher Activity
to get thedata extras
and redirect user to the desiredActivity
. Below is the code, you have to write inonCreate()
of yourActivity
to redirect user to the desiredActivity
.Another case to this type is, when your
Launcher Activity
is defined aslaunchMode="true"
in themanifest
and when theNotification
Arrives, yourLauncher Activity
is in theForeground
. So when you click on the Notification, you have toOverride
theonNewIntent()
method in yourLauncher Activity
to open the desiredActivity
. Below is the sample code for the same.So in short, I would say it's good to go with the Data Payload type as it provides more flexibility and control over the
Notification
and more importantly as we all are used toGCM
, so this type is what we all would like to prefer.Check this code and let me know.