Android,应用程序打开时显示 alertDialog 而不是通知

Android, Display alertDialog instead of notification when app is open

我遵循了 this 开发人员教程,并在我的应用程序中使用了地理围栏,正如预期的那样。

当发生地理围栏转换时,从 IntentService:

中发送通知
@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

    ...        

    sendNotification(geofenceTransitionDetails);
}

private void sendNotification(String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setContentTitle(notificationDetails)
            .setContentText("Return to app")
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

这是教程中的千篇一律。意图在 Main activity:

中设置
private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

如果应用程序打开,我如何添加抑制通知的功能,而是向用户显示 AlertDialog?理想情况下,我希望能够执行不同的任务,具体取决于发生地理围栏转换时用户当前所在的视图。我可以 monitor/intercept 从每个视图内部或以某种方式全局转换吗?

提前致谢。

a) 您可以将 activity 的生命周期事件通知您的服务。

b) 您可以将 UI 的当前状态保存在 activity 的静态字段中,并在显示通知之前从服务中进行检查。

最简单的方法是使用 LocalBroadcastManager or some event bus

因此,当转换发生时,您应该从 IntentService 发送本地广播,并在 IntentService 和任何 Activity 之间用一些 component X 捕获它。 Component X 必须跟踪您的 Activity 中是否有任何一个在前台并且

  • 如果是 - 向上传递其他本地广播(到前台Activity),
  • 如果没有 - 显示通知。

请注意,在 Android 中,如果您的应用程序在前台或不在前台,您将无法轻松跟踪(如果您有超过 1 个 Activity,我认为您无法正确执行)但是you can try.

有些答案不完整,所以这里是我正在寻找的完整解决方案。

首先,设置 MyApplication class,实现 ActivityLifecycleCallbacks:

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

    private static boolean isActive;

    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityLifecycleCallbacks(this);
    }

    public static boolean isActivityVisible(){
        return isActive;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        isActive = true;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        isActive = false;
    }

    ... no other methods need to be used, but there are more that 
    ... must be included for the ActivityLifecycleCallbacks
}

一定要在你的清单中命名它(只添加了名称行,其余是默认的):

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true">

上面所做的用于跟踪应用程序的生命周期。您可以使用它来检查您的应用程序当前是否在前台。

下一步是设置一个 BroadcastReceiver,无论你想在何处编码 运行(如果触发发生时应用程序处于打开状态)。在这种情况下,它在我的 MainActivity:

protected BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        ... Do whatever you want here

        Toast.makeText(...).show();
    }
};

在您的 onCreate 中注册接收器 activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    LocalBroadcastManager.getInstance(this).registerReceiver(mNotificationReceiver, new IntentFilter("some_custom_id"));
}

别忘了注销它:

@Override
protected void onDestroy() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mNotificationReceiver);
    super.onDestroy();
}

收到广播后,将执行接收器中的代码。

现在,检查应用程序是否在前台,如果是则发送广播。 IntentService 里面:

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = getErrorString(this,
                geofencingEvent.getErrorCode());
        return;
    }

    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        ...

        if(MyApplication.isActivityVisible()){
            Intent intnt = new Intent("some_custom_id");
            intnt.putExtra("message", geofenceTransitionDetails);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intnt);
        }else{
            sendNotification(geofenceTransitionDetails);
        }

    } else {
        // Log the error.
    }
}

重要的一点是最后一个嵌套的 if 语句:

if(MyApplication.isActivityVisible()){
    Intent intnt = new Intent("some_custom_id");
    intnt.putExtra("message", geofenceTransitionDetails);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intnt);
}else{
    sendNotification(geofenceTransitionDetails);
}

使用 MyApplication.isActivityVisible() 检查应用程序是否在前台,如上定义,然后发送通知或发送广播。只需确保您的意图代码(即 "some_custom_id")与您的发送者和接收者匹配。

仅此而已。如果应用程序在前台(特别是 MainActivity),我会执行一些代码。如果应用程序不在前台,我会发送通知。