AndroidStudio在应用程序未运行时无法接收通知负载
创始人
2024-08-20 12:30:04
0

当应用程序未运行时,Android Studio 无法接受推送通知的负载。要解决此问题,您需要在应用程序中实现 FirebaseMessagingService 类以处理通知负载。在 FirebaseMessagingService 类中,您可以使用 onMessageReceived 方法来接收通知负载并执行所需的操作。以下是一个示例:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMessagingService";

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

    // Get notification payload
    String notificationTitle = remoteMessage.getNotification().getTitle();
    String notificationBody = remoteMessage.getNotification().getBody();

    // Get data payload
    String dataValue1 = remoteMessage.getData().get("key1");
    String dataValue2 = remoteMessage.getData().get("key2");

    // Handle notification
    sendNotification(notificationTitle, notificationBody, dataValue1, dataValue2);
}

private void sendNotification(String notificationTitle, String notificationBody, String dataValue1, String dataValue2) {
    // Build notification
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(notificationTitle)
            .setContentText(notificationBody)
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true);

    // Handle data payload
    if (dataValue1 != null && dataValue2 != null) {
        // Do something with data values
    }

    // Show notification
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, notificationBuilder.build());
}

}

您还需要在 AndroidManifest.xml 文件中声明您的 FirebaseMessagingService 类,如下所示:

现在您可以在应用程序未运行时接受推送通知并处理通知负载。

相关内容

热门资讯

第8分钟了解!518互娱辅助器... 第8分钟了解!518互娱辅助器下载!都是是真的有辅助工具(确实有挂)-哔哩哔哩518互娱辅助器下载透...
第八分钟了解!雀姬辅助脚本!总... 第八分钟了解!雀姬辅助脚本!总是是有辅助方法(有挂助手)-哔哩哔哩1、起透看视 雀姬辅助脚本辅助软件...
第9分钟了解!微信财神十三章辅... 第9分钟了解!微信财神十三章辅助安装包!确实真的是有辅助教程(有挂教程)-哔哩哔哩小薇(辅助器软件下...
一分钟了解!约战武汉辅助软件!... 一分钟了解!约战武汉辅助软件!果然真的有辅助插件(有挂方针)-哔哩哔哩1、不需要AI权限,帮助你快速...
第九分钟了解!咸宁方片十三张透... 第九分钟了解!咸宁方片十三张透视脚本!本来一直总是有辅助技巧(有挂方针)-哔哩哔哩咸宁方片十三张透视...
第六分钟了解!微信小游戏辅助器... 第六分钟了解!微信小游戏辅助器!切实一直总是有辅助技巧(有挂工具)-哔哩哔哩1、打开软件启动之后找到...
第八分钟了解!蘑菇云辅助使用视... 第八分钟了解!蘑菇云辅助使用视频!原来有辅助技巧(证实有挂)-哔哩哔哩1)蘑菇云辅助使用视频辅助插件...
第九分钟了解!掌电竞技辅助工具... 第九分钟了解!掌电竞技辅助工具!其实真的是有辅助软件(有挂技术)-哔哩哔哩1、掌电竞技辅助工具有没有...
5分钟了解!佛手在线大菠萝辅助... 5分钟了解!佛手在线大菠萝辅助!本来真的有辅助软件(揭秘有挂)-哔哩哔哩1、全新机制【佛手在线大菠萝...
第2分钟了解!新永和辅助!都是... 第2分钟了解!新永和辅助!都是一直总是有辅助攻略(有挂分享)-哔哩哔哩1、这是跨平台的新永和辅助轻量...