1

Actually I'm implementing firebasePushNotification service which is working wihtout any issue . Now when I'm receiving push_notification and my app is in foreground by clicking the notification intent is being passed to the desired activity without any error. But when the app is in background or not opened clicking on the notification is not sending the intent to the desired activity and even it's not showing any error . I tried to refer some questions in SO about adding exported=true option in manifest.xml but it couldn't solve my issue.

Code of NotificationManager Class:

public void displayNotification(String title, String body,String post_owner_username,String post_owner_time,
                                String notif_commenter_username,String notif_comment_time,String follower_username,String type) {

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(ctx, Constants.CHANNEL_ID)
                    .setSmallIcon(R.drawable.new_icon)
                    .setContentTitle(title)
                    .setContentText(body);

    Intent i = new Intent(ctx, ProfileHolder.class);
    if (type.equals("likes")) {
        Bundle b = new Bundle();
        b.putString("post_owner_username", post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("what", "show_notification_post");
        i.putExtras(b);
        i.putExtra("Open", "starred");
    }
    else if (type.equals("comments")) {
        Bundle b = new Bundle();
        b.putString("post_owner_username",post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("notif_commenter_username", notif_commenter_username);
        b.putString("notif_comment_time",notif_comment_time);
        b.putString("what", "show_notification_post");
        i.putExtras(b);
        i.putExtra("Open", "starred");
    }
    else if (type.equals("comment_likes")) {
        Bundle b = new Bundle();
        b.putString("post_owner_username", post_owner_username);
        b.putString("post_owner_time", post_owner_time);
        b.putString("notif_commenter_username", notif_commenter_username);
        b.putString("notif_comment_time", notif_comment_time);
        b.putString("what", "show_notification_post");
        i.putExtras(b);
        i.putExtra("Open", "starred");
    }
    else if (type.equals("follow")||type.equals("follow_request")) {
        Bundle b = new Bundle();
        b.putString("searchUsername", follower_username);
        i.putExtras(b);
        i.putExtra("Open", "search_profile");
    }
   // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i, PendingIntent.FLAG_ONE_SHOT);

    /*
    *  Setting the pending intent to notification builder
    * */

    mBuilder.setContentIntent(pendingIntent);

    NotificationManager mNotifyMgr =
            (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);


    if (mNotifyMgr != null) {
        mBuilder.setAutoCancel(true);
        mNotifyMgr.notify(1, mBuilder.build());
    }
}
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45

2 Answers2

2

If you are using firebase push notification

You need to send a click_action attribute ...to open the desired activity when app is in background.

So try ..

 Intent i = new Intent(click_action);

click_action is the intent-filter mapping to the desired activity. click_action is mandatory

instead of the Explicit intent call - explicit intent call in the above case will only work when the app is in foreground..

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • Thanks a lot for your great answer sir . Although intent passing problem is now solved but the activity is not receiving bundle how to fix that – Srinivas Nahak Apr 17 '18 at 19:31
0

Here is how I've managed to open specific activity from notification when app is in the background/closed and to pass data through Intent.

Application

public class AppConfig extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseApp.initializeApp(this);
    }
}

Manifest

<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>

<!-- Don't forget to set exported attribute to true -->
<activity android:name=".MainActivity" android:exported="true"/>

MessagingService

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String body = null;

    if(remoteMessage.getNotification() != null) {
        body = remoteMessage.getNotification().getBody();
    }

    String id = null;

    // get data that server passed (if it passed any at all)
    if(remoteMessage.getData().size() > 0) {
        id = remoteMessage.getData().get("id");
    }

    sendNotification(body, id);
}

private void sendNotification(String messageBody, String id) {
    // put data you want to send through intent
    Bundle bundle = new Bundle();
    bundle.putString("id", id);

    // create intent, put data, and 
    Intent intent = new Intent(this, ActivityLogin.class);
    intent.putExtras(bundle);

    // IMPORTANT! clearing previous tasks so desired activity will launch
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    // customising the notification
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0, notificationBuilder.build());
}

}

jelic98
  • 723
  • 1
  • 12
  • 28