我正在从服务器向我的应用发送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);
}
有人可以帮忙吗?谢谢..
我懂了。在您的有效负载中,您同时使用notification
和data
有效负载,当应用程序在后台运行时,这会更改您应该在其中接收详细信息的位置。在我在注释中提到的文档中,您可以在摘要中看到有效载荷中是否同时包含了这两者:
数据:意图之外。
进一步来说:
在后台应用程序中处理通知消息
当您的应用程序在后台运行时,Android会将通知消息定向到系统任务栏。默认情况下,用户点击通知会打开应用启动器。
这包括同时包含通知和数据有效负载的消息(以及从Notifications控制台发送的所有消息)。在这些情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图范围内传递。
我认为@ArthurThompson的回答很好地解释了这一点:
当您发送带有数据有效负载(通知和数据)的通知消息并且应用程序处于后台时,您可以从由于用户点击通知而启动的意图的额外内容中检索数据。
在点击通知时启动MainActivity的FCM示例中:
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] 删除。
我来说两句