0

I know this is asked a million times before and I have tried everything I saw but I just cant make it work. I have data payload notification. Am getting them in onMessageReceived() but when I click the notification in notification tray it doesn't redirect to the launcher.

if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        JSONObject data = json.getJSONObject("data");
        Intent resultIntent = new Intent(getApplicationContext(), LandingActivity.class);

        Bundle bundle = new Bundle();
        bundle.putString("data", data.toString());
        resultIntent.putExtras(bundle);


            // image is present, show notification with image
           //showNotificationMessageWithBigImage(getApplicationContext(), title, message, "13", resultIntent, profilePicThumb); 
           Intent intent = new Intent(this, LandingActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("FCM Message")
                    .setContentText("my message")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

and LandingActivity is the launcher activity of my app.

my manifest.xml

<activity
        android:name=".landing.LandingActivity_"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.FullScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

When notification_payload with optional data_payload was given I got data in LandingActivity but when I get only data_payload the click redriection doesnt happen

the data-payload structure is like this

"data" :
{
"notification_type"   : "QUIZ_WINNER", 
"notification_message": "You hav won the quiz ",
"notification_data"   : [
        "user_id"          : id,
        "full_name"        : full_name,
        "profile_pic_thumb": user_profile_picthumb,
        'quiz_id'          : quiz_id,
        'question_id'      : question_id,
        'quiz_title'       : quiz_title,
    ],
"user_id"             : from_id,
'notification_to'     : to_id,
'created_at'          : date,
'updated_at'          : date,

}

I am able to receive notification but I cannot open the LandingActivity

Please help. Thanks in advance.

FINAL ANSWER

Finally I got it working..I was using android annotations throught my project. The activity which I need to open was also annotated. Hence instead of this line of code

Intent intent = new Intent(this, LandingActivity.class);

i added this

Intent intent = new Intent(this, LandingActivity_.class);

Hope it will be useful for someone at sometime.

suja
  • 1,198
  • 2
  • 12
  • 32
  • Can you show us the contents of the function showNotificationMessage and a sample of the payload, both with and without the optional data. – Jude Fernandes Oct 19 '17 at 14:45
  • @JudeFernandes please see my post. I have added the payload. showNotificationMessage() is not used any more – suja Oct 20 '17 at 05:01
  • Hi @suja Do add in an answer. Self-answering is highly encouraged here in Stack Overflow. I see that you added one earlier, you could just go ahead and undelete it, then accept it after a few hours. :) – AL. Oct 24 '17 at 11:35

1 Answers1

0

At the line you are setting 'Pending Intent', you should use a request code other than zero. It fixed my problem. I hope it will fix your problem too.

int requestCode = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
hasbi
  • 520
  • 3
  • 10