为 firebase 上的参数创建自定义数据以调用推送通知

한명훈*_*陳祖亮 4 php notifications android push-notification firebase

从 FCM onMessageReceived 方法中的 Get value from RemoteMessage 中研究了我的问题,但没有帮助

我想在 Firebase 中创建自定义数据以在我的推送通知中调用该数据。至少该数据可以调用 onMessageReceived 方法。

我的网络服务

$msg = array
(
    'body'      => "$message",
    'title'     => "Approval System",
    'sound'     => 'default',
    'vibrate'   => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'

);
$fields = array
(
    'registration_ids'  => $tokens,
    'questionTitle'     => "Test",
    'notification'      => $msg
    );


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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);           
if ($result === FALSE) {
    die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
Run Code Online (Sandbox Code Playgroud)

和我的 onMessageReceived 方法

Bundle bundle = new Bundle();
bundle.putString("DATA",messageBody);
Intent intent = new Intent(FirebaseMessage.this, ApprovalSystemActivity.class);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT
);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notif)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notif))
        .setSound(defaultSoundUri)
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(messageBody))
        .setAutoCancel(true)
        .setContentTitle("Approval System")
        .setContentText(messageBody)
        .setContentIntent(pendingIntent);

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

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)

如何将 $fields 数组上的“questionTitle”调用到 java android?如果不能理解,对不起我的问题

??

Har*_*ode 8

您必须更改 JSON 结构。将“questionTitle”字段放在“data”对象中

{
  "registration_ids": [
    "dTlWTEzmJZk:APA91bF2COvY_WPcybE6okJv2a??9qUkEUieDOP6q_Q204q1??JMz-l2Un49M5cPg4WzE5??ODTFyU_82Sz5lvR2zGO6??Al_51lY1AyfEuw890jZ9??YZ5pWn8kUjClAEwNjxBZ??8eELQABRndhQom"
  ],
  ??"notification": {
    "bod??y": "xoa",
    "title": "Ap??proval System",
    "sound": "default",
    "vibrate": 1,
    "largeIcon": "large_ico??n",
    "smallIcon": "smal??l_icon"
  },
  "data": {
    "qu??estionTitle": "Test"
  }
}
Run Code Online (Sandbox Code Playgroud)

要使此 JSON 修改您的 php 为:

$msg = array
 (
    'body'      => "$message",
    'title'     => "Approval System",
    'sound'     => 'default',
    'vibrate'   => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'

);

$dat = array
(
    'questionTitle'     => "Test"
);

$fields = array
(
    'registration_ids'  => $tokens,
    'notification'      => $msg,
    'data'       => $dat
);
Run Code Online (Sandbox Code Playgroud)

然后在你的服务类中得到它

String questionTitle = remoteMessage.getData().get("questionTitle");
Run Code Online (Sandbox Code Playgroud)