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

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

相关内容

热门资讯

曝光透视!pokemmo辅助器... 曝光透视!pokemmo辅助器,pokemmo辅助脚本(透视)好像真的是有辅助方法(哔哩哔哩)1、打...
分享透视!哈糖大菠萝挂,soh... 您好,哈糖大菠萝挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
分享透视!pokemmo手机辅... 分享透视!pokemmo手机辅助软件,佛手大菠萝有挂吗(透视)一直存在有辅助脚本(哔哩哔哩)1、上手...
专业透视!sohoo开挂辅助,... 专业透视!sohoo开挂辅助,哈糖大菠萝开挂(透视)切实一直都是有辅助攻略(哔哩哔哩)1.哈糖大菠萝...
科普透视!智星德州插件,wer... 科普透视!智星德州插件,werplan辅助软件(透视)竟然真的有辅助工具(哔哩哔哩)1、实时werp...
科普透视!约局吧德州真的有透视... 科普透视!约局吧德州真的有透视挂吗,agpoker辅助(透视)本来有辅助技巧(哔哩哔哩)1)约局吧德...
了解透视!佛手在线大菠萝辅助,... 了解透视!佛手在线大菠萝辅助,werplan透视挂(透视)竟然是有辅助方法(哔哩哔哩)该软件可以轻松...
专业透视!拱趴大菠萝挂哪里,w... 专业透视!拱趴大菠萝挂哪里,werplan有挂吗(透视)真是存在有辅助软件(哔哩哔哩);1)拱趴大菠...
了解透视!大菠萝免费辅助,聚星... 了解透视!大菠萝免费辅助,聚星ai辅助工具激活码(透视)果然一直都是有辅助app(哔哩哔哩)1、进入...
有挂透视!拱趴大菠萝怎么开挂,... 有挂透视!拱趴大菠萝怎么开挂,哈糖大菠萝有没有挂(透视)总是真的有辅助插件(哔哩哔哩)运拱趴大菠萝怎...