簡體   English   中英

當應用程序被殺死或不在前台時,FCM通知在某些設備上不起作用

[英]FCM Notification not working on some devices when application is killed or not in foreground

當應用程序被用戶殺死(用戶從應用程序托盤上滑走)或被系統OS殺死時,是否進行過使FCM通知在某些設備上起作用的研究,但是沒有找到任何解決方案,如果您可以提供解決方案或告訴我我錯了。 通知是數據有效載荷

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String payload = "",notification_message = "", channelId ="", channelName="AAA", channelDescription="";
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        String title = null, body = null;
        if(remoteMessage.getNotification() != null) {
            Log.d("REMOTE_DATA",remoteMessage.getNotification().getBody());
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
        }

        if(remoteMessage.getData() != null) {
            if(remoteMessage.getData().get("title") != null) 
               title = remoteMessage.getData().get("title");

            if(remoteMessage.getData().get("message") != null) 
               body = remoteMessage.getData().get("message");

            if(remoteMessage.getData().get("channelName") != null) 
               channelName = remoteMessage.getData().get("channelName");
        }
    }

    //Setting up Notification channels for android O and above
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        Log.e(TAG, "onMessageReceived: => " + android.os.Build.VERSION.SDK_INT);

        if(notificationChannels.containsKey(channelName)) {
            JSONObject channelData = (JSONObject) notificationChannels.get(channelName);
            try {
                channelId = channelData.getString("channelId");
                channelName = channelData.getString("channelName");
                channelDescription = channelData.getString("channelDescription");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
            if (notificationChannel == null)
                setupChannel(channelId, channelName, channelDescription);
        }
    }

    Log.d(TAG, "Notification Message Body: " + title);
    Log.d(TAG, "Notification Message Body: " + body);
    if(title != null && body != null) {
        apptivity_variable.createNotification(channelId,title, body);
        db.insertNotification(title, body, remoteMessage.getData().toString());
    }
}

我遇到了同樣的問題,經過搜索和解決后,我發現用戶需要手動啟用自動啟動和電池優化權限,有關更多詳細信息,請參閱鏈接

通常會在大多數中國手機上優化電池等問題。

鏈接將解決您的問題

FCM中有兩種不同類型的推送消息優先級:普通優先級和高優先級。 只有高優先級推送會在后台運行應用程序並將其喚醒,這是“打ze模式”限制的主題。 您可以在本文中閱讀有關它的更多信息

要提高推送消息的優先級,您必須將此部分添加到推送消息的正文中:

"android":{"priority":"high"}

有關推送消息的更多信息,請點擊此處。

嘗試注冊其他服務以在后台標識該應用以觸發該服務

public class FirebaseBackgroundService extends BroadcastReceiver{

  private static final String TAG = "FirebaseService";

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "I'm in!!!");

    if (intent.getExtras() != null) {
      for (String key : intent.getExtras().keySet()) {
        Object value = intent.getExtras().get(key);
        Log.e("FirebaseDataReceiver", "Key: " + key + " Value: " + value);
        if(key.equalsIgnoreCase("gcm.notification.body") && value != null) {
          Bundle bundle = new Bundle();
          Intent backgroundIntent = new Intent(context, BackgroundSyncJobService.class);
          bundle.putString("push_message", value + "");
          backgroundIntent.putExtras(bundle);
          context.startService(backgroundIntent);
        }
      }
    }
  }
}

另外,不要忘記在manifest.xml中注冊它

<receiver android:exported="true" android:name=".FirebaseBackgroundService" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </receiver>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM