Reputation:
I am sending a notification message from my localhost
server like this:
Javascript:
function send_notification(empno,charge,op){
return $.ajax({
url: '/notification_server/firebase_server.php',
type: 'POST',
data:{
"employee": empno,
"body":op
},
cache: false
})
}
(async() => {
await send_notification("Hi Leo","This is a test notification");
})();
PHP:
<?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
$factory = (new Factory)
->withServiceAccount('admin.json')
->withDatabaseUri("https://nfr-qtr-electric-billing-default-rtdb.firebaseio.com");
$deviceToken = '.....'; //I have my actual device token here
$messaging = $factory->createMessaging();
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification(['title' => $_POST["employee"], 'body' => $_POST["body"]]);
$messaging->send($message);
?>
I am reading the notification like this in my android
:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
String notificationTitle = null, notificationBody = null;
if (remoteMessage.getNotification() != null) {
notificationTitle = remoteMessage.getNotification().getTitle();
notificationBody = remoteMessage.getNotification().getBody();
sendLocalNotification(notificationTitle, notificationBody);
}
}
private void sendLocalNotification(String notificationTitle, String notificationBody) {
Intent intent = new Intent(this, record_viewer.class);
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)
.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.mipmap.ic_launcher) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSound(defaultSoundUri);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1234, notificationBuilder.build());
}
}
Upon executing the code, I don't receive any notifications. In the network console, it shows that the AJAX
request has been sent and there are no errors in my php
server code. I tried logging the notificationTitle
and the notificationBody
in my FirebaseMessagingService
, but it too doesn't show anything. Am I doing anything wrong? Why am I not receiving notification? Please help me.
Upvotes: 0
Views: 269
Reputation: 370
Based on your payload, you're sending data message with employee
and body
fields.
However, your message handling implementation uses getNotification() which is for notification messages. It won't return any value for notificationTitle
and notificationBody
since there's no title
and body
fields set in the payload.
You should use getData() instead. It should look something like this:
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (true) {
scheduleJob();
} else {
handleNow();
}
}
Upvotes: 1