So, there is the following situation: I send a push notification to android application as follows:
Click here for the https://fcm.googleapis.com/fcm/send request
I form the json notification itself:
{ "data": { "title": "Новое сообщение", "content" : "Привет)" }, "to": "/topics/all", "priority": "high" } (I do all this in Postman)
All devices are automatically included in the topic:
FirebaseMessaging.getInstance().subscribeToTopic("all"); The notification is fine, the application accepts it, but in the logs it writes TITLE = NULL, BODY = NULL (which is obvious, because I don't have the notification section in json).
The problem is that the onMessageReceived function is not called!
As they write in the internet: send a notification with the data section in json instead of notification, which I do, but NOTHING happens. But if sent with the notification section, then the notification draws nicely (bypassing, of course, the onMessageReceived function) ...
Here is the onMessageRecieved: (I have specified println in it to see that the function is being called)
@Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); System.out.println("+---------------------------------------------+"); System.out.println("|------------ MESSAGE RECIEEVED! -------------|"); System.out.println("+---------------------------------------------+"); sendNotification(remoteMessage); } private void sendNotification(RemoteMessage remoteMessage) { Map<String, String> data = remoteMessage.getData(); String title = data.get("title"); String message = data.get("message"); Intent intent = new Intent(this, Login.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_profile) .setWhen(System.currentTimeMillis()) .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_profile)) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setAutoCancel(false) .setSound(defaultSoundUri); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } Fragment of the manifesto (all services are registered):
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher_foregroundd" android:label="@string/app_name" android:largeHeap="true" android:roundIcon="@mipmap/ic_launcher_foregroundd" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Login"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".Service.MyFirebaseInstanceIdService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> <service android:name=".Service.MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> </application> I ask for help, otherwise I’ve already half the Internet shoveled and the result is 0. What should I do, how to be?
FirebaseMessaging.getInstance().subscribeToTopic("/topics/all");- Yuriy SPb ♦