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

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

相关内容

热门资讯

每日必看,wepoker透视挂... 每日必看,wepoker透视挂的玩法,微扑克开挂透视教程模板(往昔是有辅助)主打本地运行的 AI 黑...
玩家实测,德扑之星发牌规律图,... 玩家实测,德扑之星发牌规律图,智星菠萝德州有挂吗办法(一向真的有辅助器)是一款可以让一直输的玩家,快...
实用技巧,德扑之星能不能透视,... 实用技巧,德扑之星能不能透视,WOK有没有透视挂方案(其实是有辅助器)是一款可以让一直输的玩家,快速...
带你了解,德州辅助器免费下载最... 您好:德州辅助器免费下载最新版本这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
揭秘攻略,wepoker有没有... 您好:wepoker有没有透视挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
必看攻略,wepoker真的可... 您好:wepoker真的可以开挂透视吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会...
一分钟秒懂,wepoker开挂... 您好,wepoker开挂透视这款游戏可以开挂的,确实是有挂的,需要了解加微【485275054】很多...
攻略教程,WePoKer辅助透... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成WePoKer辅助透视软件挂解析、 ...
分享一款!,德扑之星破解透视挂... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
我来教教大家,wpk德州最新版... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wpk德州最新版解析、 AI 处理、...