Reputation: 272
I am able to send notification when app is in foreground and background. But cant manage to send it when app is killed i.e app not running in background. Other app in my mobile are able to send me notification even when they are running in background. I am using oreo version.
I too replaced 'notification' with 'data' which didnt made a difference. I already added the custom notification on onMessageReceived method, the 'notification' and 'data' both gives notification on foreground and background. Only difference is 'data' runs onMessageReceived method while on background too. But on both , notification is not received when app is killed.I have tried following code on php. What am i doing wrong?
function sendPushNotification($token) {
$url = "https://fcm.googleapis.com/fcm/send";
$serverKey = 'AAAA.....theKey';
$title = "My App";
$body = "hello there!!";
$notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
/* if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}*/
curl_close($ch);
// echo "<br>";
return $response;
}
Following in onMessageReceived method:
For 'notification':
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("apkflow","onMessageReceived Started");
if (remoteMessage.getNotification() != null) {
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
Log.d("apkflow","title = " + title);
Log.d("apkflow","body = " + body);
}
}
For 'data':
title = remoteMessage.getData().get("title");
body = remoteMessage.getData().get("body");
UPDATE:: I got the solution now !! Its due to the mobile recents updates. In mobiles like Vivo, oppo, xiomi and so on , when app is cleared, it force stop the app forcing to stop all the services. So, the FCM services is also stopped and no any notification is received on mobile. So, for getting notification, user must allow the app to run in background "allow in background" must be checked. This solves the problem. If you still have problem , leave a comment!!
Upvotes: 1
Views: 1505
Reputation: 543
Messages with both notification and data payload, when received in the background.
Change the notification
type data
$arrayToSend = array('to' => $token, 'data' => $notification,'priority'=>'high');
Please go through the below documentation https://firebase.google.com/docs/cloud-messaging/android/receive
You have to create the custom notification.
private void setNotification(RemoteMessage content) {
Log.d(TAG, "custom notification: ");
Intent intent = new Intent(this, NotificationActivity.class);
if (!content.getData().get("url").isEmpty())
intent.putExtra("url", content.getData().get("url"));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.custome_notification);
remoteViews.setTextViewText(R.id.tvTime, currentDate());
remoteViews.setTextViewText(R.id.text, content.getData().get("text"));
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getPackageName())
.setSmallIcon(R.drawable.ic_alert)
.setContent(remoteViews)
.setAutoCancel(true)
.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// To avoid replacing old notification by new one. To set new id for every new Notification following notifications.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(getPackageName(), "AppName", importance);
notificationManager.createNotificationChannel(mChannel);
}
int notifyId = (int) System.currentTimeMillis();
notificationManager.notify(notifyId, notificationBuilder.build());
}
Upvotes: 2