【问题标题】:FCM notification click_action should redirect to open browserFCM 通知 click_action 应重定向到打开浏览器
【发布时间】:2019-07-02 16:55:30
【问题描述】:

我的fcm点击操作有问题,收到通知后,当我点击它时,它应该打开浏览器,进入“www.google.co.in”,但是当我点击通知时,它会打开默认主活动。

这是我的 onMessageReceived() :

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
  Log.d(TAG, "Message Data Payload:" + remoteMessage.getData());       
  Log.d(TAG, "Message notification body: " + remoteMessage.getNotification().getBody());
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setData(Uri.parse("www.google.co.in"));
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_stat_ic_notification)
                    .setContentTitle("FCM Message")
                    .setContentText("FCM MessageBody")
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

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

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

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

有人知道这段代码的具体实现是什么或有什么问题吗?

提前致谢。

【问题讨论】:

    标签: android firebase android-studio android-intent firebase-cloud-messaging


    【解决方案1】:

    为了更好地理解这个话题,我建议你阅读这篇文章 firebase cloud messaging

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      Log.i(TAG, "Message Data Payload:" + remoteMessage.getData());       
      Log.i(TAG, "Message notification body: " + remoteMessage.getNotification().getBody());
    
            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    
                switch (remoteMessage.getNotification().getBody()) {
    
                    case "openUrlGoogle":
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse("www.google.co.in"));
                        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
                        break;
    
                    case "openUrlOtherSite":
                        break;
    
                    default:
                        Log.v("TAG", "error");
                        break;
                }
    
            }
    
        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle("FCM Message")
                        .setContentText("FCM MessageBody")
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
    
        notificationManager.notify(0, notificationBuilder.build());
    }
    

    【讨论】:

    • 我阅读了 FCM 文档并进行了搜索,上面的代码仅在应用程序处于前台时有效,但如果应用程序处于后台/被杀死,fcm 会将数据消息发送到默认活动,我们必须使用 Intent 的附加功能进行检索,我参考了此链接 - firebase.google.com/docs/cloud-messaging/android/…
    • 所以当应用程序在后台/被杀死时不会调用 onMessageReceived()。
    • 即使代码仅在应用程序处于前台时设置自定义通知,当我点击它并且通知消失时它也不会重定向。
    • 我还面临一个问题,即当应用程序被终止时,当用户点击通知时,没有调用 onMessageReceived()。 @Satish 你找到解决方案了吗?
    • 试试这个@SharathHuddar dontkillmyapp.com
    猜你喜欢
    • 2022-08-15
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2020-05-05
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多