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,
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());
}
}
}
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
© 2024 OneMinuteCode. All rights reserved.