Umair Solis
Umair Solis

Reputation: 108

FCM notification doesn't show notification pop up in background

I'm using firebase cloud function for showing notification in my app and that working perfectly only issue I'm facing is when app is closed or in background notification doesn't show pop up. Heads up notifications are used in app and I do get the notification but notification doesn't pop up as it do while app is in foreground.I have set the Priority to high for both channel and notification but still it doesn't work.

My Service Class :

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.app_logo)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true).setContentIntent(pendingIntent)
                .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_MAX);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(true);
             channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
        }
        manager.notify(0, builder.build());
    }

Manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="com.noderon.riscalert.WebViewActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.noderon.riscalert.MainActivity">

        </activity>

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

    </application>

Any help would be appreciated. Thanku

Upvotes: 5

Views: 8601

Answers (3)

Umair Iqbal
Umair Iqbal

Reputation: 2069

As you are already setting the Priorities it seems like you are not adding the metadata for the notification and when the app is in the background it won't get any data that's why it only showing the pop up in the foreground add these metadata in your code:

<meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_stat_ic_notification" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

Also, you can see Google Cloud Messaging documentation for more details

Happy coding

Upvotes: 8

Aasima Jamal
Aasima Jamal

Reputation: 119

Have you created FirebaseMessage Instance() in your Activity?

Upvotes: 0

Priyanka
Priyanka

Reputation: 1875

I have faced the same issue. I have followed the official documentation from the firebase and now notifications are working fine. Follow this link .

Upvotes: 0

Related Questions