I have implemented the clickAction
of Firebase that is included in the payload, that will be handled by the Intent Filter of android manifest.
The .FcmNotificationTray
Activity will be executed, once the notification is clicked on foreground or in background state of the application. But when the application is killed or forced closed, Assuming we have still remaining notification on system tray, the activity will be executed once clicked, but for the second time the user select and click the notification it will not execute.
So to summarize it, when the user select and open a notification for the second time (state was when app is killed), it will not execute the activity, more likely that the clickAction
has no control to the intent filter.
Payload:
{
"notification": {
"alert": "sample Alert | Green",
"data": "Timestamp:\n 2016/09/04 13:16:44,
"sound": "default",
"gcmNotification": {
"title": "Sample App,
"body": "Sample Body | Red",
"timeToLive": 10,
"clickAction": "FIREBASE_ACTIVITY"
}
},
"users": [
"demo@firebase.com"
]
}
Intent-filter :
<activity
android:name=".activities.FcmNotificationTray"
android:label="TITLE"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="FIREBASE_ACTIVITY"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
EDIT 1: This is my code for the activity that will be executed once the user clicks the notification.
public class FcmNotificationTray extends AppCompatActivity {
AppPreferences mPreferences;
StringProcessor mStringsplit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onShowPushNotifDialog();
}
@Override
public void onResume(){
super.onResume();
onShowPushNotifDialog();
}
private void onShowPushNotifDialog(){
/* Instantiate Preferences */
mPreferences = new AppPreferences(this);
/* Get data from notification tray center for the push notification */
Bundle bundle = getIntent().getExtras();
/* Call method to split string and set data for shared preference */
mStringsplit = new StringProcessor();
mStringsplit.onCreateSplitString(bundle.getString("data"),bundle.getString("alert"),this);
/* This will be executed if the app is killed or no current activity */
if (isTaskRoot()) {
Intent intent = new Intent(this,SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{ finish();}
}
}
EDIT 2 : Code for FCM Receiver
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
/* Call Function to show notification */
showPushNotification(remoteMessage.getData().get("data"),
remoteMessage.getData().get("alert"));
}
}
/* For Push Notification */
public void showPushNotification(String data,String alert){
/* Call method to split string and set data for shared preference */
mStringsplit = new StringProcessor();
mStringsplit.onCreateSplitString(data,alert,getApplicationContext());
/* Get shared Preferences for app in background or foreground */
mPreference = new AppPreferences(getApplicationContext());
Boolean actVisibility = mPreference.getVisiblityActivity();
if(actVisibility) {
/* If application is in foreground execute the dialog
* The purpose of executing an acitivity is to get the context
*/
Intent intentdemo = new Intent().setClassName(getPackageName(),getPackageName()
+ mPreference.getClassName());
intentdemo.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentdemo.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentdemo.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intentdemo);
}