How do I enable Firebase Notification to receive push notifications even if the application is forced to stop?

Asked 2 years ago, Updated 2 years ago, 55 views

I created and sent a message from the Notification tab of the Firebase Console to verify that I can receive the message.
The message to be sent specifies a message statement and label, and targets the user segment.
https://gyazo.com/a0d5dd07be0f31e1b3f6da7f0bd56158

As stated in the document,

  • If the app is in foreground (Activity is displayed)
  • If the app is in the background (with Activity hidden by pressing the home button)

I was able to receive it when
However, if you forcefully stop the application, you will not receive any notifications.
(Forced stop was done by setting the terminal → applicable application → forced stop)
The logcat displays the following error message:

W/GCM-DMM:broadcast intent callback:result=CANCELLED forIntent {act=com.google.android.c2dm.int.RECEIVE flg=0x10000000 pkg=jp.gcreate.sample.samplefirebasenotification(hasextras)}

How can I get notifications even if the application is terminated?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="jp.gcreate.sample.samplefirebasenotification"
    >

<application
    android:allowBackup="true"
    android: icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportRtl="true"
    android:theme="@style/AppTheme"
    >
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <service
        android:name = ".MyFirebaseMessagingService"
        >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
</application>

</manifest>

FirebaseMessagingService Extended Class

public class MyFirebaseMessagingService extensions FirebaseMessagingService{
    private static final String TAG = "FirebaseMessage";

    @ Override
    public void onMessageReceived (RemoteMessage remoteMessage) {
        super.onMessageReceived (remoteMessage);

        Log.d(TAG, "onMessageReceived:" + remoteMessage);

        if(remoteMessage.getNotification()!=null){
            Log.d(TAG, "Message Notification Body:" + remoteMessage.getNotification().getBody());
            RemoteMessage.Notification notification=remoteMessage.getNotification();
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentText(notification.getBody())
                   .setContentTitle(notification.getTitle())
                   .setSmallIcon(R.mipmap.ic_launcher);
            NotificationManagerCompatManagerCompat=NotificationManagerCompat.from(this);
            managerCompat.notify(0, builder.build());
        }
    }
}

android firebase

2022-09-30 15:50

1 Answers

What terminal do you use?
Some devices, such as Huawei, do not display notifications unless they are protected apps.

Settings > Advanced Settings > Battery Manager > Protected Apps


2022-09-30 15:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.