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

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

相关内容

热门资讯

第9分钟带你了解!潮汕馆辅助(... 第9分钟带你了解!潮汕馆辅助(透视挂)揭秘一下外挂开挂辅助下载(其实真的有挂)《详细加薇136704...
一分钟了解"皮皮胡子... 一分钟了解"皮皮胡子辅助"开挂(透视)辅助神器(果然是真的有挂的)-哔哩哔哩,皮皮胡子辅助是用手机号...
2分钟带你了解!广东雀神挂机怎... 广东雀神挂机怎么样 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: ...
分享实测"小程序牵手... 分享实测"小程序牵手跑得快辅助器"开挂(透视)辅助脚本(原来是真的有挂的)-哔哩哔哩;是一款可以让一...
一分钟带你了解!新道游辅助器透... 一分钟带你了解!新道游辅助器透视挂(辅助挂)每日必看外挂开挂辅助平台(果然有挂)新道游辅助器透视挂a...
最新技巧"丽水都来大... 最新技巧"丽水都来大菠萝脚本辅助"开挂(透视)辅助脚本(原来确实有挂的)-哔哩哔哩;丽水都来大菠萝脚...
第7分钟带你了解!手机科乐辅助... 第7分钟带你了解!手机科乐辅助(辅助挂)重大来袭外挂开挂辅助工具(一贯有挂)>>您好:软件加薇136...
透视挂"闲逸app官... 透视挂"闲逸app官方免费版下载"开挂(透视)辅助平台(一直是真的有挂的)-哔哩哔哩;相信小伙伴都知...
第6分钟带你了解!新518互游... 第6分钟带你了解!新518互游辅助(辅助挂)一分钟教会你外挂开挂辅助插件(一贯有挂)新518互游辅助...
透视黑科技"掌中乐游... 透视黑科技"掌中乐游戏修改器"开挂(透视)辅助工具(果然确实有挂的)-哔哩哔哩;亲真的是有正版授权,...