Android app receives push notification via FCM but doesn’t display a message

I implemented Push Notification through FCM. When the application receives a new notification from my server, the Notification Bar bar is marked with the icon that I set in NotificationCompat.Builder, but the message does not appear as a pop-up window. I tried to set priority, style, category, but still the notification is not shown. When I scroll, I see a notification.

I tried the application on two different OS devices (6.0.1 and 5.0.1). Also My backend C # solution - both do not report notification - message and notification

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theApp"
android:versionCode="31"
android:versionName="0.3.4"  >
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />
   <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission    android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="com.theApp.SplashScreen"
        android:screenOrientation="portrait"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>



    <activity
        android:name="com.theApp.MainView"
        android:windowSoftInputMode="adjustResize"
     android:screenOrientation="portrait"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.NoActionBar" >
    </activity>


    <service android:name=".FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service android:name=".FirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>


</application>




</manifest>

FirebaseMessagingService

package com.theApp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessagingService extends    com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    showNotification(remoteMessage.getData().get("message"));
}
public void showNotification(String message)
{
    Intent i =new Intent(this,MainView.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //FLAG_ACTIVITY_CLEAR_TOP

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(true)
            .setContentTitle("Title")
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(message))
            .setContentText(message)
            .setTicker(message)
            .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pendingIntent)
            .setCategory(NotificationCompat.CATEGORY_PROMO);

        NotificationManager manager =   (NotificationManager)getApplicationContext().getSystemService(Context.NOTI      FICATION_SERVICE);
        manager.notify(0,builder.build());

   }
}

Backend (C #):

// Data message
  var contentData = new
            {
                registration_ids = tokens,
                priority = "high",
                data = new
                {                       
                    title = "this is title",
                    body = "this is body"
                }
            };

            // Notification message
            var contentData = new
            {
                registration_ids = tokens,
                priority = "high",
                notification = new
                {
                    title = title,
                    body = message
                }
            };
+4
1

, remoteMessage.getNotification(), remoteMessage.getData(). , 'notification' , , . getData() . . . FCM

-1

Source: https://habr.com/ru/post/1649944/


All Articles