I’m encountering an issue with sending notifications to my Android app despite having implemented all necessary additions.
I developed a WebView application and followed the common practices found on various internet sources for implementing notifications. Firstly, in MainActivity.java
, within the onCreate
method, I invoked:
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
System.out.println("Fetching FCM registration token failed");
return;
}
// Get new FCM registration token
String token = task.getResult();
// Log and toast
}
});
Furthermore, I made the necessary additions to the MyFirebaseMessagingService.java
file as depicted in various online resources:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
System.out.println("From: " + remoteMessage.getFrom());
if (remoteMessage.getNotification() != null) {
System.out.println("Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
}
private void sendNotification(String from,String body){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
}
});
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
String channelId = "notification";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle("TeknoFore")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
I also declared the service in AndroidManifest.xml
as follows:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Additionally, I ensured that the POST_NOTIFICATIONS
permission was granted and placed the google-services.json
file in the app directory. However, despite these steps, neither the test notifications sent via token nor the notifications sent directly to the application are being received.
What could be causing this issue, and how can I resolve it? Any insights would be greatly appreciated. Thank you.
2
Answers
Update: Yes this is a bit strange but the problem was suddenly solved (I didn't do anything extra) I think it was a timing issue. In this case I found out that the steps I was taking were correct, so I mark the issue as solved.
Try changing
to
in one of the examples I read that notificationId should be positive
Notifications for a channel can be disabled immediately after creation, possibly due to the NotificationManager.IMPORTANCE_DEFAULT flag
This happened on my Xiaomi phone, and so did one other person.