android - 使用 setGroup() 的 Kitkat(API 19)中的堆栈通知不起作用

标签 android notifications android-4.4-kitkat

我有一个找不到答案的问题。我试过 AndroidDeveloper 教程,我在 stackoverflow 和谷歌上搜索过,但要么我的搜索技能太棒了,要么我认为没有答案可以解决我的问题。

当有多个消息时,我想将所有新消息的消息通知堆叠为一个通知。我可以为每条消息显示一个通知,但我不能进行堆栈/摘要通知。

我得到的最好的是:http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png

我想要的是:http://developer.android.com/images/android-5.0/notifications/Summarise_Do.png

我使用的是 API 19,它不需要向后兼容。在 AndroidStudio 中编写我的代码并在 Genymotion 中进行测试。

我已尝试使用 NotificationCompatManager 并获得多个通知:NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

我也尝试过使用 NotificationManager 得到相同的结果,即多个通知:

NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

我的通知是这样的:

通知 notification = new NotificationCompat.Builder(this) .setContentTitle( "新消息来自...") .setContentText("消息:...") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION)) .setGroup(mNoti) .setGroupSummary(真) .setAutoCancel(真) .build();

然后我像这样触发通知:

notificationManager.notify(counter, notification);

每个通知的计数器递增,因此每个通知都有自己的 ID。

我也尝试过这种形式来构建和发送通知但没有成功:

  NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("message from...")
                    .setContentText("message...")
                    .setContentIntent(pIntent)
                    .setAutoCancel(true)
                    .setGroup(mNoti)
                    .setGroupSummary(true)
                    .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION));


    notificationManager.notify(counter, notificationBuilder.build());

setGroup 显然没有注意到我。我不明白为什么它不起作用或我应该如何让它起作用。

我的组是这样的:

final static String mNoti = "mNoti";

唯一接近我想要的是对所有通知使用相同的 ID,以便新通知覆盖旧通知,并在构建通知时使用 setNumber(int)。但是,这并不能真正给我想要的结果,因为我认为我无法获得用户未处理的通知数量。或者我可以吗?

任何人都可以帮助我实现我想要的目标吗?

最佳答案

您获得结果的原因如 http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png是因为您将所有通知的组摘要设置为 true。在 API 文档中,它被描述为:将此通知设置为一组通知的组摘要。这意味着每个通知都将被视为摘要,因此将显示为新通知。

要解决您的问题,您可以在每次收到新通知时发送新的摘要。请注意,我不是 100% 确定这是最佳 解决方案,但它解决了问题。 我创建了一个测试应用程序,它根据按钮点击添加通知:

public class MyActivity extends Activity {

    private final static String GROUP_KEY_MESSAGES = "group_key_messages";
    public static int notificationId = 0;
    private NotificationManagerCompat manager;


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

        manager = NotificationManagerCompat.from(this);
        manager.cancelAll();
    }

    public void addNotification(View v) {
        notificationId++; // Increment ID

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        Intent viewIntent = new Intent(this, ShowNotificationActivity.class);
        viewIntent.putExtra("notificationId", notificationId); // Put the notificationId as a variable for debugging purposes

        PendingIntent viewPendingIntent = PendingIntent.getActivity(this, notificationId, viewIntent, 0); // Intent to next activity

        // Group notification that will be visible on the phone
        Notification summary = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(viewPendingIntent)
                .setContentTitle("New notifications")
                .setContentText("You have "+notificationId+" new messages")
                .setLargeIcon(icon)
                .setGroup(GROUP_KEY_MESSAGES)
                .setGroupSummary(true)
                .build();

        manager.cancelAll(); // Is this really needed?
        manager.notify(notificationId, summary); // Show this summary
    }
}

显示通知 Activity :

public class ShowNotificationActivity extends Activity {

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

        Intent intent = getIntent();
        int counter = intent.getIntExtra("COUNTER", -57);
        Log.d("COUNTER", ""+counter);

        TextView view = (TextView)(findViewById(R.id.textView));
        view.setText("You have just read "+counter+" messages");
        MyActivity.notificationId = 0; // Reset ID. Should not be done like this, but used to show functionality.
    }
}

第一个 Activity MyActivity 用于处理通知。我想这主要是在接收器中完成的。 MyActivity 中的一个按钮会触发 addNotification,它会推送新的通知摘要。在添加摘要之前,所有旧摘要都将被取消。单击摘要时,您将进入名为 ShowNotificationActivty 的启动 Activity ,其中显示了数据。例如,如果这是一个电子邮件应用程序,MyActivity 将是某种电子邮件接收器,它将电子邮件保存到数据库中。 notificationId 可以设置为保存在数据库中的某个 id。点击通知后,可以根据notificationId查看邮件。

希望对您有所帮助:)

关于android - 使用 setGroup() 的 Kitkat(API 19)中的堆栈通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487846/

相关文章:

Android Maps API2 发布 key 在本地工作但不能通过 Play 商店工作

java - 当 Android Studio 遇到代理内的断点时应用程序崩溃

android - 如何在android中闪烁通知图标? [完毕]

android - 在 Android 通知中隐藏时间而不使用自定义布局

android - Android KitKat 是否允许支持蓝牙 LE 的设备充当外围设备?

我的 AsyncTask 中的 java.lang.NullPointerException

android - 使用 HTTP 请求处理飞行模式

android - 远程服务异常 : Bad notification for statForeground - no notifications in app

android - 如何彻底退出沉浸式全屏模式?

android - Android 4.4.2-Youtube Webview仅播放视频声音