android - Gmail 类似通知功能 android

标签 android android-notifications

我需要实现像 GMAIL 这样的通知功能,即如果屏幕当前打开,列表应该自动刷新并且不会收到通知。如果应用程序关闭,我们会收到通知。在 GMAIL 中,如果我们在收件箱中并且新邮件到达,它会自动附加在顶部而不通知,如果应用程序未打开,我们会收到新邮件通知。 我已经对通知部分进行了编码并且它工作正常,但唯一的问题是即使我正在进行当前 Activity ,我仍然会收到我试图停止的通知。

下面是 Intent 类发送通知的代码

Intent listIntent = new Intent(this, ListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ListActivity.class);
stackBuilder.addNextIntent(listIntent);

PendingIntent listPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("xxxx")
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText("Update List"))
                .setContentText("Update List").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis());
        builder.setContentIntent(listPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, builder.build());

我在网络和 SO 上进行了很多研究,但没有得到任何相关的指示。 CommonsWare 似乎有一个解决方案,但我不想使用任何外部库。同样在我的研究过程中,我发现知道哪个 Activity 在最前面并不是生产中的好习惯,所以不能用它来停止通知是我当前的 Activity 在最上面。请指教。

最佳答案

这个解决了你的问题

 ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
            boolean isActivityFound = false;

            if (services.get(0).topActivity.getPackageName().toString().equalsIgnoreCase(getPackageName().toString()))
            {
                isActivityFound = true;
            }
            if (!isActivityFound)
            {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("xxxx")
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText("Update List"))
            .setContentText("Update List").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis());
    builder.setContentIntent(listPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, builder.build());
            }       

关于android - Gmail 类似通知功能 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27634602/

相关文章:

android - SherlockFragment onCreateOptionsMenu 没有调用

Android:为什么我无法更改 EditText 相对于另一个 EditText 的值?

java - 通知与行动

android - 如何让通知置顶?

android - android 计算 session 长度

带有每日通知的 Android 应用

android - 通知抽屉打开后无法继续播放通知声音

java - GPS 坐标未正确存储在从 android 到 php 的数据库中

android - 更改部分textview的背景颜色

java - 发出 URL 请求并获取 JSON 响应的最简单方法是什么?