I researched from Get value from RemoteMessage from FCM onMessageReceived method for my problem but not helped
I want to create custom data in Firebase to call that data on my push notification. At least that data can call on onMessageReceived method.
My webservice
$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;
and my onMessageReceived method
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());
how to i can call "questionTitle" on $fields array to java android ? sorry for my question if cannot understand
谢谢
You have to change your JSON structure. Put the "questionTitle" field inside a "data" object
{
"registration_ids": [
"dTlWTEzmJZk:APA91bF2COvY_WPcybE6okJv2a9qUkEUieDOP6q_Q204q1JMz-l2Un49M5cPg4WzE5ODTFyU_82Sz5lvR2zGO6Al_51lY1AyfEuw890jZ9YZ5pWn8kUjClAEwNjxBZ8eELQABRndhQom"
],
"notification": {
"body": "xoa",
"title": "Approval System",
"sound": "default",
"vibrate": 1,
"largeIcon": "large_icon",
"smallIcon": "small_icon"
},
"data": {
"questionTitle": "Test"
}
}
To make this JSON modify your php to:
$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
);
And then in your Service class get it by
String questionTitle = remoteMessage.getData().get("questionTitle");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With