简体   繁体   中英

How to fix notification Firebase FCM not recieved after app sleeping 1 min

i have an problem on my app dating i use function cloud firebase for send an notification but notification not recived after 1 min sleeping app. just when i click on button for turn phone on notificcation recieved. and i see when app is connected with cable usb notifications showing always this note i think is battry. so i will share my code below if anyone have any idea THANKS

Manifest :

<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

 <service
        android:name=".Utils.Notify"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

Notify service :

public class Notify extends com.google.firebase.messaging.FirebaseMessagingService {

String CHANNEL_ID = "my_channel_010555";            // The id of the channel.
DataFire dataFire;
int i=0;


@Override
public void onNewToken(@NonNull String s) {
    super.onNewToken(s);
}


@Override
public void onMessageReceived(@NotNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    FirebaseDatabase.getInstance().getReference().child("testnotifi").setValue("good");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(time,broadcast1),broadcast1);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), broadcast1);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), broadcast1);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), broadcast1);
    }

    dataFire=new DataFire();

    /////// first you need to create in file index.js  //////////////////
    final String from_user_id = remoteMessage.getData().get("userIDvisited");
    final String state_app = remoteMessage.getData().get("state_app");
    final String notification_state = remoteMessage.getData().get("notification_state");

    notification(remoteMessage,from_user_id,notification_state);


}

private  void notification(RemoteMessage remoteMessage, String from_user_id, String notification_state){
    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.piece_of_cake);

    String notificationTitle = remoteMessage.getData().get("title");
    String notificationMessage = remoteMessage.getData().get("body");
    //String click_action = remoteMessage.getNotification().getClickAction();

    @SuppressLint("WrongConstant") NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setBadgeIconType(R.drawable.luul_icon)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.luul_icon))
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.luul_icon)
                    .setContentTitle(notificationTitle)
                    .setContentText(notificationMessage)
                    .setVibrate(new long[]{0, 500, 1000})
                    .setDefaults(Notification.DEFAULT_LIGHTS)
                    .setSound(soundUri);


    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mBuilder.setSmallIcon(R.drawable.ic_notify_app);
        mBuilder.setColor(getColor(R.color.colorPrimary));
    } else {
        mBuilder.setSmallIcon(R.drawable.luul_icon);
    }


    Intent resultIntent = new Intent("com.xxx.xxx_TARGET_NOTIFICATION_messages");
    resultIntent.putExtra("userIDvisited", from_user_id);
    resultIntent.putExtra("notification_state", notification_state);

    /////////// Because clicking the notification opens a new ("special") activity, there's
    /////////// no need to create an artificial back stack.
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    //////////////////////////////////
    mBuilder.setContentIntent(resultPendingIntent);

    // Sets an ID for the notification
    int mNotificationId = (int) System.currentTimeMillis();
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        if (mNotifyMgr != null) {
            List<NotificationChannel> channelList = mNotifyMgr.getNotificationChannels();

            for (int i = 0; channelList != null && i < channelList.size(); i++) {
                mNotifyMgr.deleteNotificationChannel(channelList.get(i).getId());
            }
        }

        CharSequence name = "NotifyChannelWork";                   
        String descrition = "NotifyChannelDescWork";                   
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(descrition);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setSound(soundUri,
                new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setLegacyStreamType(AudioManager.STREAM_RING)
                        .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build());

        if (mNotifyMgr != null)
            mNotifyMgr.createNotificationChannel(channel);
    }

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    @SuppressLint("InvalidWakeLockTag")
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "MyWakelockTag");
    wakeLock.acquire();


    //to release the screen lock
    KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
    //add permission in the manifest file for the disablekeyguard
    keyguardLock.disableKeyguard();

    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

}

Function using Node.js index.js:

 if(notification_state=="message"){
                
                                    const payload = {

                                            data:{
                                                    title : `${username}`,
                                                    body  : `${last_message}`,
                                                    userIDvisited : from_user_id,
                                                    state_app:state_app,
                                                    notification_state:notification_state,
                                                    priority:"high"
                                                }
                                            };

                                    return admin.messaging().sendToDevice(token_id,payload).then(response => {
                                        console.log('this was the notification Feature ');

                                    });
                    }

Reviewing Google Firebase Cloud Messaging , I found solutions to Handle notification messages in a background app and Background restricted app .

Handling notifications messages in a background app:

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Background restricted app:

FCM may not deliver messages to apps which were put into background restriction by the user (such as via: Setting -> Apps and Notification -> [appname] -> Battery). Once your app is removed from background restriction, new messages to the app will be delivered as before. To prevent lost messages and other background restriction impacts, make sure to avoid bad behaviors listed by the Android vitals effort.

Solution:

I've whitelisted the Google Play Services from battery optimization, so it will never die and programmatically adjust WIFI settings to run even in deep sleep. Now the watch is receiving notifications even though it's in sleep mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM