How to get data sent from server using fcm if app is in background?(如果应用程序在后台,如何使用 fcm 从服务器发送数据?)
问题描述
我正在从服务器向我的应用程序发送 fcm 通知.
I am sending fcm notifications to my app from server.
我正在从包含 user_id 的服务器发送数据.如果应用程序在前台,我会在 FirebaseMessageService 类中获取此 userId.但是当应用程序在后台时没有得到它.因为 FirebaseMessagingService 类仅在应用程序处于前台时才会执行.
I am sending data from server which contains user_id. I am getting this userId in FirebaseMessageService class if app is in foreground. But Not getting it when the app is in background. As FirebaseMessagingService class only gets execute when the app is in foreground.
那么当应用在后台时,我怎样才能得到这个id呢?
So how can I get this id when the app is in background?
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());
}
}
当应用处于后台时,我仍在使用数据有效负载 onMessageReceived 不会被调用.
I am using data payload still onMessageReceived dose not get called when app is in background.
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);
}
有人可以帮忙吗?谢谢..
Can anyone help with this please? Thank you..
推荐答案
我明白了.在您的有效负载中,您同时使用 notification
和 data
有效负载,当应用程序处于后台时,它会更改您应该接收详细信息的位置.在我在评论中提到的 doc 中,您可以在摘要中看到如果两者都包含在有效负载中:
I see. In your payload you are using both notification
and data
payload, which changes where you're supposed to receive the details when the app is in background. In the doc I mentioned in the comments, you can see in the summary if both are included in the payload:
数据:额外的意图.
更具体地说:
在后台应用中处理通知消息
当您的应用处于后台时,Android 会将通知消息定向到系统托盘.默认情况下,用户点击通知会打开应用启动器.
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
这包括包含通知和数据负载的消息(以及从通知控制台发送的所有消息).在这些情况下,通知会发送到设备的系统托盘,数据负载会在启动器 Activity 的 Intent 的附加部分中发送.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
我认为@ArthurThompson 的这个答案解释得很好:
I think this answer by @ArthurThompson explains it very well:
当您发送带有数据负载(通知和数据)的通知消息并且应用程序在后台时,您可以从由于用户点击通知而启动的额外意图中检索数据.
When you send a notification message with a data payload (notification and data) and the app is in the background you can retrieve the data from the extras of the intent that is launched as a result of the user tapping on the notification.
来自 FCM 示例 在点击通知时启动 MainActivity:
From the FCM sample which launches the MainActivity when the notification is tapped:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
这篇关于如果应用程序在后台,如何使用 fcm 从服务器发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果应用程序在后台,如何使用 fcm 从服务器发送
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01