3

I know there are multiple posts like this, but I have tried many solutions and nothing works for me.

I am using a Firebase Cloud Messaging to send notifications. The notification arrives, but it opens the main activity, not the one that I want.

I try and try, but keep failing with starting appropriate activity. Tried different Intent or PendingIntent settings, tried click_action, nothing works for me.

I think that it is not even entering my FirebaseMessagingService, but I did everything like in the Firebase instruction.

manifest:

  <service
            android:name="com.packagename.FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

 <activity android:name="com.packagename.NotificationActivity">
            <action android:name="OPEN_ACTIVITY_1" />
            <category android:name="android.intent.category.DEFAULT" />
        </activity>

FirebaseMessagingService:

    public class FirebaseMessagingService extends FirebaseMessagingService {

    private static String TAG = "Firebase";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }


        if(remoteMessage.getNotification()!=null){
            Log.d(TAG,"Message body : "+remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
        }
    }

    private void sendNotification(String body, String title) {
        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notificationIntent.putExtra("notification", body);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notification)
                        .setContentTitle(getString(R.string.app_name))
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(body)
                        .setBigContentTitle(title))
                        .setContentText(body)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notification.build());
    }
}

Logs after click:

D/StatusBar: Clicked on content of 0|com.mypackagename|0|GCM-Notification:89893881|10177 , PendingIntent{cc15013: android.os.BinderProxy@e3031fc} , Intent { act=com.google.firebase.MESSAGING_EVENT cmp=com.mypackagename/com.google.firebase.iid.FirebaseInstanceIdInternalReceiver (has extras) }
Piotr Sagalara
  • 2,247
  • 3
  • 22
  • 25
  • try calling sendNotification() method outside if condition because getNotification() may return null if app is in background. – Murli Prajapati Apr 06 '17 at 19:46
  • See this: http://stackoverflow.com/questions/37565599/firebase-cloud-messaging-fcm-launch-activity-when-user-clicks-the-notificati – rafsanahmad007 Apr 06 '17 at 19:51
  • The code looks okay to me. Could you post your Server side code or a sample payload? – AL. Apr 07 '17 at 02:54
  • sendNotification() outside if didn't help, I've added logs which are showing after click on notification. I don't have any other logs with "Firebase", so it seems that it doesn't even enter my FirebaseMessagingService (or just doesn't show logs in logcat). – Piotr Sagalara Apr 07 '17 at 07:14

1 Answers1

5

If app in background, notifications directs to the system tray.

After click notification it will redirect to launcher activity which is given in manifest file.

From launcher activity we can call any activity but first it will goto launcher activity.

In Launcher activity we can get notification message contents .

AndroidManifest.xml

 <activity

            android:name=".view.SplashActivity"

            android:screenOrientation="portrait">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

SplashActivity.Java

public class SplashActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        Bundle bundle = getIntent().getExtras();

        if (bundle != null && bundle.get("data")!=null) {


    //here can get notification message
          String datas = bundle.get("data").toString();

    }
}
}
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19