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

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

相关内容

热门资讯

重大推荐!决战卡五星辅助源码(... 重大推荐!决战卡五星辅助源码(透视)新老夫子脚本(详细开挂外开挂教程)1)决战卡五星辅助源码辅助挂:...
一分钟了解!!鱼乐吧游戏脚本(... 一分钟了解!!鱼乐吧游戏脚本(透视)拱趴大菠萝有辅助吗(详细开挂外开挂教程);1、首先打开拱趴大菠萝...
实测必看!!GG大玩家辅助软件... 实测必看!!GG大玩家辅助软件(透视)潮汕激k辅助(详细开挂外开挂教程);1)GG大玩家辅助软件辅助...
信息共享!微信填大坑辅助(透视... 信息共享!微信填大坑辅助(透视)传送屋激k看底牌辅助软件(详细开挂外开挂教程)1、传送屋激k看底牌辅...
分享个大家!微信途游有辅助吗(... 分享个大家!微信途游有辅助吗(透视)大菠萝挂机辅助(详细开挂外开挂教程);1、该软件可以轻松地帮助玩...
每日必备!!陕西三代辅助器免费... 每日必备!!陕西三代辅助器免费试用(透视)杭州都莱辅助(详细开挂外开挂教程)1、陕西三代辅助器免费试...
必知教程!!贰柒拾手机辅助(透... 必知教程!!贰柒拾手机辅助(透视)衢州都莱有没有挂(详细开挂外开挂教程)1、构建自己的贰柒拾手机辅助...
推荐几款新版!杭州都莱辅助臁(... 推荐几款新版!杭州都莱辅助臁(透视)阿拉斗牌作弊(详细开挂外开挂教程)1、杭州都莱辅助臁系统规律教程...
必知教程!!微信小程序游戏破解... 必知教程!!微信小程序游戏破解器(透视)蜀山四川免费辅助器(详细开挂外开挂教程)1、许多玩家不知道微...
必看攻略!好友赣南新版本辅助(... 必看攻略!好友赣南新版本辅助(透视)潮汕掌上娱破解(详细开挂外开挂教程)1、起透看视 好友赣南新版本...