当前位置: 首页 > 知识库问答 >
问题:

当应用程序在后台时FCM不工作的意图(android)

钮刚洁
2023-03-14

我正在使用FCM推送通知。当应用程序处于前台时,应用程序运行良好,并打算启动新活动,但当应用程序处于后台时,它不启动新活动,而是启动默认活动的实例

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional







    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, SecActivity.class);
    intent.putExtra("started_from","notification");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 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("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

}

共有1个答案

甘学潞
2023-03-14

希望您正在尝试在收到消息时启动mainactivity。当应用程序从后台恢复时,您当前的活动将被清除。flag_activity_clear_top的文档:如果设置了,并且正在启动的活动已经在当前任务中运行,则不启动该活动的新实例,而是关闭该活动顶部的所有其他活动,并将此意图作为新意图传递给(现在位于顶部的)旧活动。

请尝试删除此标志。

 类似资料: