Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Last active October 15, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/b10aba769371e63cf4b99e47f0e446a7 to your computer and use it in GitHub Desktop.
Save tzkmx/b10aba769371e63cf4b99e47f0e446a7 to your computer and use it in GitHub Desktop.
Background Push Notification

Send title and message in data rather than notification

Only foreground receives:

{
   "to": "the device token"
   "notification": {
     "title":"New Notification!",
     "body":"Test"
   },
   "priority":10
 }

For background handling:

{
  "to": "the device token"
  "data": {
    "title":"New Notification!",
    "body":"Test"
  },
  "priority":10
}

References

public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(123, notification);
}
}
<?php
$fcmNote = FcmNotification::create()
->setTitle('Aviso')
->setBody($notificationBody)
;
// add notification data to receive in background
$pushData['title'] = 'Aviso';
$pushData['message'] = $notificationBody;
$message = FcmMessage::create()
->setPriority(FcmMessage::PRIORITY_NORMAL)
->setTimeToLive(86400)
->setNotification($fcmNote)
// add data array to send in data field
->setData($pushData)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment