首页 > 解决方案 > Firebase 数据通知无法点击

问题描述

嗨,我想在通知上发送图像并使用数据通知这是我的数据,通知带有图片。但未点击通知我如何发送可点击的?

  ""data"" : {""click_action"":"".MainActivity"",
 ""body"" : ""new Symulti update 22!"", 
 ""title"" : ""new"", 
""url"":""https://www.blablaaas.com"",
""img_url"":""https://www.image.com/image1""
""}}

标签: androidfirebasefirebase-cloud-messagingfirebase-notifications

解决方案


我希望您正在为 android 实现代码,因此要使通知可点击,您只需添加带有目标 Activity 名称的pendingIntent,这样您就可以获得可点击的操作。使用下面的代码作为参考。

 try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent mainIntent = new Intent(this, TabHostScreen.class);
    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent mainPIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
    builder.setSmallIcon(getNotificationIcon(builder));
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setAutoCancel(true);
    //start intent on notification tap (MainActivity)
    builder.setContentIntent(mainPIntent);
    //custom style
    builder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
    builder.setCustomContentView(remoteCollapsedViews);
    //builder.setCustomBigContentView(remoteExpandedViews);
    long[] pattern = {500, 500, 500};
    builder.setVibrate(pattern);
    Random rand = new Random();
    NOTIFICATION_ID = rand.nextInt(1000);
    //notification manager
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());

推荐阅读