【问题标题】:Did not get Notification in Android mobile when Application removed from the Recent Used Apps从最近使用的应用程序中删除应用程序时,在 Android 手机中未收到通知
【发布时间】:2017-07-30 15:55:03
【问题描述】:

我正在使用 firebase 云消息向我的应用程序发送通知。当我的应用程序在前台并切换到另一个应用程序时我收到通知(我在后台运行的应用程序未关闭)。 但是当我从最近使用的应用程序中删除时。我没有收到通知。 我也得到了令牌 ID。问题是从最新使用的应用中删除应用时没有收到通知。

Android 清单文件: 内部应用

  <service
        android:name="com.mysite.android.abc.service.MyFirebaseMessagingService"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>

        </intent-filter>
    </service>
    <!-- [END firebase_service] -->
    <!-- [START firebase_iid_service] -->
    <service
        android:name="com.mysite.android.abc.service.MyFirebaseInstanceIDService"

        >
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <!-- [END firebase_iid_service] -->
    <receiver
        android:name=".service.FirebaseDataReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

MyFirebaseInstanceIDService:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    // Notify UI that registration has completed, so the progress indicator can be hidden.
    Intent registrationComplete = new Intent( Config.REGISTRATION_COMPLETE);
    registrationComplete.putExtra("token", refreshedToken);
    LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);

}
}

MyFirebaseMessagingService:

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived( remoteMessage );
    if(remoteMessage==null){
        return;
    }
    if (remoteMessage.getNotification()!=null) {
        handleNotification( remoteMessage.getNotification().getBody() );

    }
    if(remoteMessage.getData().size()>0){

        try {
            JSONObject jsonObject=new JSONObject( remoteMessage.getData().toString() );
            handleDataMessage( jsonObject );


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
private void handleNotification(String message){
    if(!NotificationUtils.isAppIsInBackground( getApplicationContext() )){
        Intent pushNotification=new Intent( Config.PUSH_NOTIFICATION );
        pushNotification.putExtra( "message",message );
        LocalBroadcastManager.getInstance( this ).sendBroadcast( pushNotification );
        // play notification sound
        /*NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();*/
    }else{

    }
}
private void handleDataMessage(JSONObject jsonObject){
    Log.e("GGGGG mm",jsonObject.toString());
    try {
        JSONObject data=jsonObject.getJSONObject( "data" );
        String title=data.getString( "title" );
        String message=data.getString( "message" );
        JSONObject payload = data.getJSONObject("payload");
        if(!NotificationUtils.isAppIsInBackground( getApplicationContext() )){
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            /*NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();*/
        } else {
            // app is in background, show the notification in notification tray
            Intent resultIntent = new Intent(getApplicationContext(), Dashboard.class);
            resultIntent.putExtra("message", message);

            // check for image attachment
            if (TextUtils.isEmpty(imageUrl)) {

                showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
            } else {
                // image is present, show notification with image
                showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
            }
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }

}

/**
 * Showing notification with text only
 */
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}

/**
 * Showing notification with text and image
 */
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
}

【问题讨论】:

  • 当您从堆栈中删除应用程序时,您可以在 luncher 活动类中收到通知。
  • 没有。我没有得到。
  • @Shailesh 这用于通知应用程序问题。我的问题是没有收到通知

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

FCM 不支持您所询问的内容(这曾经与 GCM 一起使用,现在已弃用)

https://developer.android.com/about/versions/android-3.1#launchcontrols:

请注意,系统将 FLAG_EXCLUDE_STOPPED_PACKAGES 添加到所有 广播意图。它这样做是为了防止来自后台的广播 无意或不必要地启动组件的服务 停止的应用程序。后台服务或应用程序可以 通过添加 FLAG_INCLUDE_STOPPED_PACKAGES 覆盖此行为 用于广播应允许激活的意图的标志已停止 应用程序。

应用程序在首次安装时处于停止状态,但 尚未启动,当它们被用户手动停止时 (在管理应用程序中)。

广播 FCM 触发器已将 FLAG_EXCLUDE_STOPPED_PACKAGES 设置为 TRUE,如果用户强制停止应用(如您所做的那样),它实际上不会唤醒您的应用。

有人要求 firebase 允许用户控制此标志 - 但这是开发团队的 shot down

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2014-07-16
    相关资源
    最近更新 更多