Nik*_*ika 7 notifications android 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 notification = new NotificationCompat.Builder(this)
.setContentTitle( "New message from... ")
.setContentText("Message:...")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION))
.setGroup(mNoti)
.setGroupSummary(true)
.setAutoCancel(true)
.build();
我点这样的通知:
notificationManager.notify(counter, notification);
Run Code Online (Sandbox Code Playgroud)
如果每个通知的计数器递增,则每个通知都有自己的ID.
我也试过这个表单来构建和激活通知,而不是succsess:
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());
Run Code Online (Sandbox Code Playgroud)
setGroup显然对我有所帮助.我不明白为什么它不起作用或我应该如何使它工作.
我的小组看起来像这样:
final static String mNoti = "mNoti";
Run Code Online (Sandbox Code Playgroud)
唯一远远接近我想要的是对所有通知使用相同的ID,以便新通知覆盖旧通知,并在构建通知时使用setNumber(int).然而,这并没有真正给我我想要的结果,因为我认为我不能得到用户未处理的通知数量.或者我可以吗?
任何人都可以帮我达到我想要的目标吗?
您获得http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png中的结果的原因是因为您在所有通知上将组摘要设置为 true。在 API 文档中,它被描述为:Set this notification to be the group summary for a group of notifications。这意味着每个通知都将被视为摘要,因此将显示为新通知。
为了解决您的问题,您可以在每次收到新通知时发送新的摘要。请注意,我不能 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
}
}
Run Code Online (Sandbox Code Playgroud)
显示通知活动:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
第一个活动MyActivity用于处理通知。我想这主要是在接收器中完成的。MyActivity 中的按钮会触发 addNotification,从而推送新的通知摘要。在添加摘要之前,所有旧摘要都会被取消。单击摘要时,您将进入启动活动,称为ShowNotificationActivty数据显示位置。例如,如果这是一个电子邮件应用程序,MyActivity那么它将是某种将电子邮件保存到数据库的电子邮件接收器。notificationId 可以设置为数据库中保存的某个 id。当点击通知时,可以根据notificationId查看邮件。
我希望这有帮助 :)
| 归档时间: |
|
| 查看次数: |
6649 次 |
| 最近记录: |