当应用程序处于后台时,Firebase 推送通知 activity 未打开?

Firebase push notification activity is not opening when app is in background?

实际上我正在实施 firebasePushNotification 服务,该服务没有任何问题。现在,当我收到 push_notification 并且我的应用程序通过单击通知意图处于前台时,通知意图被传递到所需的 activity 而没有任何错误。但是,当应用程序处于后台或未打开时,单击通知不会将意图发送到所需的 activity,甚至不会显示任何错误。我试图在 SO 中提及一些关于在 manifest.xml 中添加 exported=true 选项的问题,但它无法解决我的问题。

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());
    }
}

这是我如何在应用程序处于 background/closed 时从通知中打开特定 activity 并通过 Intent.

传递数据的方法

申请

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

清单

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

消息服务

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());
}

}

如果您正在使用 firebase push notification

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

所以试试..

 Intent i = new Intent(click_action);

click_actionintent-filter 到所需 activity 的映射。 click_action 是必须的

而不是显式意图调用 - 在上述情况下,显式意图调用将在应用程序处于前台时工作。