1

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);
    }
Takao Baltazar
  • 319
  • 4
  • 17
  • You can check this thread. It has description of fcm message bundle and data http://stackoverflow.com/questions/39046270/google-fcm-getintent-dont-returning-expected-data-when-app-is-in-background-stat – Md. Sajedul Karim Oct 18 '16 at 05:55
  • Thanks for the link. But I have a different scenario, wherein my application is killed. My activity is executed on foreground and background. – Takao Baltazar Oct 18 '16 at 06:21
  • Could you add the receiver code to the question? Also the payload you are using is not in the expected FCM format. – Arthur Thompson Oct 19 '16 at 02:51
  • Hi Arthur, I've added the fcm receiver under Edit 2. The payload I was using is exclusive for the serving I am currently using which is HCPms. – Takao Baltazar Oct 19 '16 at 03:56
  • I'm wondering if you've found a solution to this in the meantime? – arekolek Jul 07 '21 at 17:37
  • Looks like the issue is that Firebase uses `FLAG_ONE_SHOT` for the `PendingIntent` instead of `FLAG_UPDATE_CURRENT`. – arekolek Jul 07 '21 at 19:46

0 Answers0