Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在任何活动中处理Firebase通知_Android_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Android 在任何活动中处理Firebase通知

Android 在任何活动中处理Firebase通知,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我遵循了本教程: 我能够向Android应用程序发送Firebase通知 扩展Firebase消息服务的类是 public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotificatio

我遵循了本教程:

我能够向Android应用程序发送Firebase通知

扩展Firebase消息服务的类是

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage.getNotification() != null) {
            String notificationTitle = remoteMessage.getNotification().getTitle();
            String notificationBody = remoteMessage.getNotification().getBody();
        handleNotification(notificationTitle, notificationBody);
    }

    private void handleNotification(String title, String body) {
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("title", title);
            pushNotification.putExtra("message", body);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

        } else {
            // If the app is in background, firebase itself handles the notification
        }
    }
}
在我的一个活动中,我定义了一个侦听器,所有其他活动都扩展了这个类

public class DisplayNotificationActivity extends AppCompatActivity {
    private BroadcastReceiver mRegistrationBroadcastReceiver;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_notification);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // process notification
            }
        }
    }
}
扩展DisplayNotificationActivity的类示例如下:

public class ListNotificationsActivity extends DisplayNotificationActivity {

    private BroadcastReceiver mRegistrationBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_notifications);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // process notification
            }
        };
    }
}
如果我在收到通知时处于DisplayNotificationActivity活动中,一切正常,但如果我处于ListNotificationsActivity等其他活动中,则调用DisplayNotificationActivity onCreate方法,而不是活动活动中的onCreate方法

如何让当前活动处理任何活动的通知接收,而不在所有活动中重复相同的代码

编辑:我暂停并恢复活动,此代码位于DisplayNotificationActivity中

@Override
protected void onResume() {
    super.onResume();
            LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(Config.REGISTRATION_COMPLETE));
            LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(Config.PUSH_NOTIFICATION));
            NotificationUtils.clearNotifications(getApplicationContext());
}

@Override
protected void onPause() {
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
    super.onPause();
}

您的activity
DisplayNotificationActivity
是activity
ListNotificationsActivity
的超类,并且您在
ListNotificationsActivity
中调用了
super.onCreate()
方法,它肯定会在创建您的超类时调用super方法,即
DisplayNotificationActivity

现在,对于解决方案,您可以使用界面:

    public abstract class DisplayNotificationActivity extends AppCompatActivity {
    private BroadcastReceiver mRegistrationBroadcastReceiver;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_notification);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // process notification
                   onNotificationReceived(your notification message);
            }
        }
    }
     public void onNotificationRecieved(String notiMessage);
 }
在您的
列表NotificationsActivity
上:

public class ListNotificationsActivity extends DisplayNotificationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_notifications);
    }

    @Override
    public void onNotificationRecieved(String notiMessage) {
        //process your notification
    }
}

我还没有运行代码,看看这是否有效。

您的活动
DisplayNotificationActivity
是活动
ListNotificationsActivity
的超级类,您已经调用了
super.onCreate()
ListNotificationsActivity
中的
方法,该方法在创建超类时肯定会调用super方法,即
DisplayNotificationActivity

现在,对于解决方案,您可以使用界面:

    public abstract class DisplayNotificationActivity extends AppCompatActivity {
    private BroadcastReceiver mRegistrationBroadcastReceiver;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_notification);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // process notification
                   onNotificationReceived(your notification message);
            }
        }
    }
     public void onNotificationRecieved(String notiMessage);
 }
在您的
列表NotificationsActivity
上:

public class ListNotificationsActivity extends DisplayNotificationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_notifications);
    }

    @Override
    public void onNotificationRecieved(String notiMessage) {
        //process your notification
    }
}

我还并没有运行代码,看看这是否有效。

在每个活动的onPause中注销接收器,并在每个活动的onResume中注册它。它会帮助你的。因为当您将进入ListNotificationsActivity时,注册的接收者将被注销DisplayNotificationActivity。我编辑了我的帖子-我正在暂停/恢复通过
BaseActivity
扩展您的每个活动,从中执行该过程。在每个活动的暂停中注销接收者,并在每个活动的恢复中注册它活动。它会帮助你的。因为当您进入ListNotificationsActivity时,注册的接收者将注销DisplayNotificationActivity。我编辑了我的帖子-我正在暂停/恢复扩展您的每个活动,方法是
BaseActivity
从中执行此过程。这仍然意味着我必须在每个活动中重复处理通知的过程活动您不必在处理消息的每个活动中都设置广播接收器。如果这不是您想要的,那么您可以在MyFirebaseMessagingService中处理通知为什么需要BroadCastReceiver?这仍然意味着我必须在每个活动上重复处理通知的过程您不必在每个活动中设置广播接收器您只需处理消息。如果这不是您想要的,那么您可以在MyFirebaseMessagingService中处理通知为什么需要BroadCastReceiver呢?