如何隐藏特定活动的通知?

问题描述 投票:0回答:3

当应用程序处于前台或后台时,我收到来自fcm的通知。我不想显示通知当特定活动中的应用程序如何在应用程序处于特定活动时禁用通知。

我正在使用datapaylod进行通知

android firebase push-notification
3个回答
0
投票

试试这个,

  private void sendNotification(String msg) {
    Intent notificationIntent = null;
    notificationIntent = new Intent(currentactivity.this, Main2Activity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    PendingIntent contentIntent = PendingIntent.getActivity(GCMNotificationIntentService.this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) GCMNotificationIntentService.this.getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = GCMNotificationIntentService.this.getResources();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(GCMNotificationIntentService.this);
    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.app_logo_small_trans)
            .setColor(getResources().getColor(R.color.app_logo_color))
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_logo))
            .setTicker(msg)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle("Propreka")
            .setLights(0xffff0000, 100, 2000)
            .setPriority(Notification.DEFAULT_SOUND)
            .setContentText(msg);

    Notification n = builder.getNotification();

    n.defaults |= Notification.DEFAULT_ALL;
    nm.notify(0, n);

}

0
投票

所以你想知道目前哪个(如果有的话)活动在前台。并且根据类类型,要限制通知的显示。

你需要自己保持这个。最简单的方法是编写一个新类并在其中包含公共静态函数,以将活动标记为活动/非活动

public static Activity mCurrentActivity = null;

public static void SetActivity(Activtiy activity) { mCurrentActivity = activity };

public static void ClearActivity(Activtiy activity) 
{ 
    if (mCurrentActivity.equals(activity))
         mCurrentActivity = null;
}

public static Activity GetCurrentActivity() { return mCurrentActivity; }

有了这样的infra之后,你可以分别在所有onResume和onPause覆盖中调用SetActivity和ClearActivity函数。

最后,在通知构建器类中,您将获得当前活动的实例并比较.class以检查您所需的活动是否是焦点。如果Current活动返回null,则表示您的应用程序不在前台。

有更好,更有效和更清洁的方法来做到这一点。例如。 Daggr的依赖注入。


0
投票

您可以通过以下方式查看当前热门活

public static Activity getActivity() {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);

Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
if (activities == null)
    return null;

for (Object activityRecord : activities.values()) {
    Class activityRecordClass = activityRecord.getClass();
    Field pausedField = activityRecordClass.getDeclaredField("paused");
    pausedField.setAccessible(true);
    if (!pausedField.getBoolean(activityRecord)) {
        Field activityField = activityRecordClass.getDeclaredField("activity");
        activityField.setAccessible(true);
        Activity activity = (Activity) activityField.get(activityRecord);
        return activity;
    }
}

return null;
}

您可以检查顶级活动是否是您不想显示通知的活动,然后从服务返回呼叫。

© www.soinside.com 2019 - 2024. All rights reserved.