搭建fcm中转服务器,如果应用程序在后台,如何使用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());

}

}

编辑:

我正在使用仍然处于onMessageReceived的数据有效负载,当应用程序在后台时不会被调用。

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);

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值