I am sending fcm notifications to my app from server.
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.
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());
}
}
EDIT:
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..
I see. In your payload you are using both
notification
anddata
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:More specifically:
I think this answer by @ArthurThompson explains it very well:
Register your service in manifest
you can check app is in Foreground or not
and add this code inside
onMessageReceived