如果应用程序在后台,如何使用fcm从服务器发送数据?

席德

我正在从服务器向我的应用发送fcm通知。

我正在从包含user_id的服务器发送数据。如果应用程序在前台,我将在FirebaseMessageService类中获取此userId。但是当应用程序在后台运行时却没有得到它。由于FirebaseMessagingService类仅在应用程序处于前台时才执行。

那么当应用程序在后台运行时,如何获取此ID?

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    private String mUserId;
    private Boolean mUpdateNotification;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        String clickAction = remoteMessage.getNotification().getClickAction();

        mUserId = remoteMessage.getData().get("user_id");

        String title = remoteMessage.getNotification().getTitle();

        //Calling method to generate notification
        sendNotification(remoteMessage.getNotification().getBody(),clickAction,title);
    }

    //This method is only generating push notification
    //It is same as we did in earlier posts
    private void sendNotification(String messageBody,String clickAction,String title) {

        mUpdateNotification = true;

        Intent intent = new Intent(clickAction);

        intent.putExtra("userId",mUserId);
        intent.putExtra("updateNotification",mUpdateNotification);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }
}

编辑:

我在应用处于后台时仍在调用onDataReceived的数据有效载荷,但未调用。

 public function sendPush($text, $tokens, $apiKey,$user_id)
{

    $notification = array(
        "title" => "User updated profile.",
        "text" => $text,
        'vibrate' => 3,
        "click_action" => "OPEN_ACTIVITY_2",
        'sound' => "default",
        'user_id' => $user_id
    );

    $data = array("user_id" => $user_id);

    $msg = array
    (
        'message' => $text,
        'title' => 'User updated profile.',
        'tickerText' => 'New Message',
    );
    $fields = array
    (
        'to' => $tokens,
        'data' => $data,
        'notification' => $notification
    );

    $headers = array
    (
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    //  echo($result);
    //    return $result;
    curl_close($ch);
}

有人可以帮忙吗?谢谢..

AL。

我懂了。在您的有效负载中,您同时使用notificationdata有效负载,当应用程序在后台运行时,这会更改您应该在其中接收详细信息的位置。我在注释中提到文档中,您可以在摘要中看到有效载荷中是否同时包含了这两者:

数据:意图之外。

进一步来说:

在后台应用程序中处理通知消息

当您的应用程序在后台运行时,Android会将通知消息定向到系统任务栏。默认情况下,用户点击通知会打开应用启动器。

这包括同时包含通知和数据有效负载的消息(以及从Notifications控制台发送的所有消息)。在这些情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图范围内传递。

我认为@ArthurThompson的回答很好地解释了这一点:

当您发送带有数据有效负载(通知和数据)的通知消息并且应用程序处于后台时,您可以从由于用户点击通知而启动的意图的额外内容中检索数据。

点击通知时启动MainActivityFCM示例中:

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        String value = getIntent().getExtras().getString(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当应用程序在iOS 7中在后台运行时将数据发送到服务器

来自分类Dev

关闭应用程序后,如何在后台连接到服务器?

来自分类Dev

在移动应用程序上无需服务器即可在后台发送电子邮件

来自分类Dev

永远在后台运行Swift 2.0应用程序以更新服务器位置

来自分类Dev

在后台运行应用程序时ping服务器

来自分类Dev

如何使用 pybluez 构建处理从 Raspberry Pi 发送的数据的 Android 蓝牙服务器应用程序

来自分类Dev

如果用户不使用应用程序,则终止 Android 计时器(防止在后台运行)

来自分类Dev

如何从桌面应用程序向Meteor服务器发送数据更新?

来自分类Dev

如何从桌面应用程序向Meteor服务器发送数据更新?

来自分类Dev

如何触发应用程序将数据发送到我的服务器?

来自分类Dev

如何在 post 方法中将数据从 android 应用程序发送到服务器?

来自分类Dev

继续在后台运行iOS应用,从加速器收集数据并将其发送到服务器

来自分类Dev

在Phonegap iOS中实现终止应用程序/在后台运行时将数据上传到服务器的方法

来自分类Dev

如何使用Firebase从服务器向应用程序发送推送通知?

来自分类Dev

当应用程序在后台时,从广播接收器向服务发送 Intent 时出现 NullPointerException

来自分类Dev

服务器无法使用从Electron应用程序发送的应用程序/ xml请求

来自分类Dev

当应用程序在后台运行时,Android中未收到FCM数据消息

来自分类Dev

当应用程序在后台时,Android中未收到FCM数据消息

来自分类Dev

在后台发送电子邮件或向服务器发送数据

来自分类Dev

在后台服务上使用应用程序资源而不打开应用程序

来自分类Dev

如果应用程序在终止期间发送http请求,服务器会收到吗?

来自分类Dev

如何通过服务器向应用程序发送任何类型的数据(例如图像,文本)?

来自分类Dev

如何将Windows Phone 8应用程序数据发送到服务器

来自分类Dev

如果使用Java接收到数据,如何制作一个从服务器接收数据并将数据发送到服务器的程序

来自分类Dev

每x秒或每分钟在后台将数据发送到服务器

来自分类Dev

当应用程序处于后台时,将纬度和经度发送到服务器

来自分类Dev

当应用程序处于后台时,将纬度和经度发送到服务器

来自分类Dev

即使应用程序在后台,如何保持后台计时器在本机运行

来自分类Dev

如何从应用程序的服务器端向浏览器JavaScript发送事件?

Related 相关文章

  1. 1

    当应用程序在iOS 7中在后台运行时将数据发送到服务器

  2. 2

    关闭应用程序后,如何在后台连接到服务器?

  3. 3

    在移动应用程序上无需服务器即可在后台发送电子邮件

  4. 4

    永远在后台运行Swift 2.0应用程序以更新服务器位置

  5. 5

    在后台运行应用程序时ping服务器

  6. 6

    如何使用 pybluez 构建处理从 Raspberry Pi 发送的数据的 Android 蓝牙服务器应用程序

  7. 7

    如果用户不使用应用程序,则终止 Android 计时器(防止在后台运行)

  8. 8

    如何从桌面应用程序向Meteor服务器发送数据更新?

  9. 9

    如何从桌面应用程序向Meteor服务器发送数据更新?

  10. 10

    如何触发应用程序将数据发送到我的服务器?

  11. 11

    如何在 post 方法中将数据从 android 应用程序发送到服务器?

  12. 12

    继续在后台运行iOS应用,从加速器收集数据并将其发送到服务器

  13. 13

    在Phonegap iOS中实现终止应用程序/在后台运行时将数据上传到服务器的方法

  14. 14

    如何使用Firebase从服务器向应用程序发送推送通知?

  15. 15

    当应用程序在后台时,从广播接收器向服务发送 Intent 时出现 NullPointerException

  16. 16

    服务器无法使用从Electron应用程序发送的应用程序/ xml请求

  17. 17

    当应用程序在后台运行时,Android中未收到FCM数据消息

  18. 18

    当应用程序在后台时,Android中未收到FCM数据消息

  19. 19

    在后台发送电子邮件或向服务器发送数据

  20. 20

    在后台服务上使用应用程序资源而不打开应用程序

  21. 21

    如果应用程序在终止期间发送http请求,服务器会收到吗?

  22. 22

    如何通过服务器向应用程序发送任何类型的数据(例如图像,文本)?

  23. 23

    如何将Windows Phone 8应用程序数据发送到服务器

  24. 24

    如果使用Java接收到数据,如何制作一个从服务器接收数据并将数据发送到服务器的程序

  25. 25

    每x秒或每分钟在后台将数据发送到服务器

  26. 26

    当应用程序处于后台时,将纬度和经度发送到服务器

  27. 27

    当应用程序处于后台时,将纬度和经度发送到服务器

  28. 28

    即使应用程序在后台,如何保持后台计时器在本机运行

  29. 29

    如何从应用程序的服务器端向浏览器JavaScript发送事件?

热门标签

归档