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 类,如下所示:

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

相关内容

热门资讯

解谜透视!sohoo开挂辅助!... 解谜透视!sohoo开挂辅助!其实真的有辅助方法(有挂教学)-哔哩哔哩1、游戏颠覆性的策略玩法,独创...
分享透视!竞技联盟辅助插件!总... 分享透视!竞技联盟辅助插件!总是是有辅助脚本(真的有挂)-哔哩哔哩所有人都在同一条线上,像星星一样排...
解迷透视!cloudpoker... 解迷透视!cloudpoker外开挂!真是一直都是有辅助脚本(发现有挂)-哔哩哔哩1、cloudpo...
有挂透视!pokemomo辅助... 有挂透视!pokemomo辅助软件!其实存在有辅助软件(有挂技巧)-哔哩哔哩1、每一步都需要思考,不...
总结透视!哈糖大菠萝开挂!竟然... 总结透视!哈糖大菠萝开挂!竟然真的有辅助教程(讲解有挂)-哔哩哔哩运哈糖大菠萝开挂辅助工具,进入游戏...
必备透视!pokemmo辅助官... 必备透视!pokemmo辅助官网!好像一直总是有辅助教程(有挂解惑)-哔哩哔哩运pokemmo辅助官...
专业透视!来玩app破解!一直... 专业透视!来玩app破解!一直是有辅助技巧(有挂详细)-哔哩哔哩1、每一步都需要思考,不同水平的挑战...
辅助透视!wepoker透视有... 辅助透视!wepoker透视有吗!竟然是真的有辅助工具(有挂教程)-哔哩哔哩1、wepoker透视有...
辅助透视!sohoo poke... 辅助透视!sohoo poker辅助!好像一直总是有辅助软件(有挂教程)-哔哩哔哩1、超多福利:超高...
曝光透视!竞技联盟透视!切实是... 曝光透视!竞技联盟透视!切实是有辅助方法(有挂方法)-哔哩哔哩1、完成竞技联盟透视有辅助插件,帮助玩...