单击通知FCM时未打开所需的活动

时间:2018-11-03 08:31:41

标签: android push-notification firebase-cloud-messaging

我正在FCM上进行演示,如果我单击通知,它将进入通知类并显示通知消息。但是每当我单击通知时,它总是重定向到默认启动器活动,即MainActivity。我见过像我这样的重复问题,但没有适当的解决方案。无论我发现什么解决方案,我都尝试过那些也不起作用的方法。请查看我的意图代码,让我知道我在哪里做错了,或者我想在代码中添加其他内容。

Intent resultIntent = new Intent(mCtx, NotificationActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent,
        PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);

mBuilder.setContentIntent(pendingIntent);

在清单中我也喜欢

  android:exported="true"

但仍然每次都重定向到MainActivity。

3 个答案:

答案 0 :(得分:0)

从后端发送的通知有效载荷存在问题。如果data有效负载到达,FCM将打开默认的启动程序活动,而notification有效负载将使您启动自定义活动。现在,您的onMessageReceived方法没有被调用。 有关更多详细说明,请参见此answer

答案 1 :(得分:0)

按照此步骤,从中删除不必要的代码。它来自一个活动的项目,可能会帮助您解决所缺少的内容。

fun createNormalNotification(notiId: Int, context: Context,
                                 toClass: Class<out Activity>,
                                 title: String, content: String,
                                 smallIcon: Int) {
        val intent = Intent(context, toClass)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        val pendingIntent = PendingIntent.getActivity(context, notiId
                , intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager

        creatingNotificationChannel(notificationManager, P.getChannelId(context), P
                .getChannelName(context),
                P.getChannelDescription(context))
        val pattern = longArrayOf(500, 500, 500, 500, 500, 500, 500, 500, 500)
        val mBuilder = NotificationCompat.Builder(context, P.getChannelId(context))
                .setSmallIcon(smallIcon)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setVibrate(pattern)
                .setAutoCancel(true);

        val notification = mBuilder.build()
        notification.flags = Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT
        notificationManager.notify(notiId, mBuilder.build());
    }


@TargetApi(Build.VERSION_CODES.O)
private fun creatingNotificationChannel(notificationManager: NotificationManager,
                                            channelId: String, channelName: String,
                                            channelDescription: String) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            val name: CharSequence = channelName;
            val description = channelDescription
            val channel = NotificationChannel(channelId, name, NotificationManager
                    .IMPORTANCE_DEFAULT);
            channel.description = description;
            // Register the channel with the system

            notificationManager.createNotificationChannel(channel);
        }
    }

答案 2 :(得分:0)

您可以尝试以下代码:-

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private String TAG = getClass().getSimpleName();


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.e(TAG, "FROM>>" + remoteMessage.getFrom());

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Message Body>>" + remoteMessage.getData());
    }

    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Message Body>>" + remoteMessage.getNotification().getBody());
        Log.e(TAG, "Message title---------------->>" + remoteMessage.getNotification().getTitle());
        Log.e(TAG, "Message Body----------------->>" + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
    }
}

private void sendNotification(String tital, String body) {
    Intent intent = null;


    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Your Notification")
            .setContentText(body)
            .setSound(uri)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(body))
            .setAutoCancel(true);

    Log.e(TAG, "Message Body---" + body);
    Log.e(TAG, "Title--" + tital);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());

 }
}