首页 > 解决方案 > 当应用程序在后台时,如何将收到的来自 Firebase 远程消息的推送通知存储到 sqlite 数据库中?

问题描述

我已经尝试过此代码。但即使我无法在 logcat 中打印,我也无法处理远程消息。

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("token",s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage.getData().size() > 0){

        Log.d("remoteMsg",remoteMessage.getData().toString());

        String title,message,Id;

        Map data = remoteMessage.getData();

        title = data.get("title").toString();
        message = data.get("body").toString();
        Id = data.get("NotifyId_Enc").toString();


        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);

            Log.d("fol", String.valueOf(json));
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }

        Intent intent = new Intent(this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setContentText(Id);
        notificationBuilder.setSound(soundUri);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);


}else {
        Log.d("remoteMsg","not working");
    }

}

我正在从 .net Web 服务器发送通知并接收 fcm 远程消息,但我无法处理它。在 android 设备中收到推送通知。

我的问题是关于如何处理远程消息..仅此而已

标签: androidsqlitefirebasepush-notificationmessages

解决方案


推荐阅读