Pav*_*hov 8 android android-notifications
但我的通知没有分组.我做错了什么?
我的代码:
private static int id =0;
final static String GROUP_KEY_GUEST = "group_key_guest";
private static void generateNotification(Context context, String message) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Intent intent = new Intent(context,MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
.setTicker("New Message")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Message")
.setContentText(message)
.setGroup(GROUP_KEY_GUEST)
.build();
notificationManager.notify(id++, notification);
}
Run Code Online (Sandbox Code Playgroud)
方法调用:
@Override
protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "????????? ?????????: " + intent.getExtras());
String message = intent.getStringExtra("content");
generateNotification(context, message);
}
Run Code Online (Sandbox Code Playgroud)
编辑,完整代码:这是我生成通知的服务
package com.managment.pavel.managmentgradle;
public class GCMIntentService extends GCMBaseIntentService {
private static int id =0;
final static String GROUP_KEY_GUEST = "group_key_guest";
NotificationManagerCompat notificationManager;
public GCMIntentService() {
super(SENDER_ID);
notificationManager = NotificationManagerCompat.from(getApplicationContext());
}
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getStringExtra("content");
generateNotification(context, message);
}
@Override
protected void onError(Context context, String s) {
Toast.makeText(context,"Error",Toast.LENGTH_LONG).show();
}
@Override
protected void onRegistered(Context context, String registrationId) {
ServerUtilities.register(context, registrationId);
}
@Override
protected void onUnregistered(Context context, String registrationId) {
ServerUtilities.unregister(context, registrationId);
}
private void generateNotification(Context context, String message) {
Intent intent = new Intent(context,MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
.setTicker("????? ?????????")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("?????????")
.setContentText(message)
.setGroup(GROUP_KEY_GUEST)
.build();
notificationManager.notify(id++, notification);
}
}
Run Code Online (Sandbox Code Playgroud)
您必须创建包含上一个详细信息的摘要通知.这个摘要通知是可见的,而不是之前的通知.它是这样的:
private static int id =0;
private static int unread_notif = 0;
final static String GROUP_KEY_GUEST = "group_key_guest";
private static void generateNotification(Context context, String message) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Intent intent = new Intent(context,MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
.setTicker("New Message")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Message")
.setContentText(message)
.setGroup(GROUP_KEY_GUEST)
.build();
notificationManager.notify(id++, notification);
unread_notif++;
if (unread_notif>1) {
Notification summaryNotification = new NotificationCompat.Builder(mContext)
.setContentTitle("Your summary message")
.setSmallIcon(R.drawable.ic_small_icon)
.setLargeIcon(largeIcon)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Details about your first notification")
.addLine("Details about your second notification")
.setBigContentTitle(Integer.toString(unread_notif)+" new notifications")
.setSummaryText("More details in app"))
.setGroup(GROUP_KEY_GUEST)
.setGroupSummary(true)
.build();
notificationManager.notify(id++, summaryNotification);
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅添加摘要通知中的文档
如果NotificationManagerCompat实例为null,请尝试在generateNotification中初始化NotificationManagerCompat并从默认构造函数中删除初始化代码:
\n\nprivate void generateNotification(Context context, String message) {\n if(notificationManager==null){\n notificationManager = NotificationManagerCompat.from(context);\n }\n\n Intent intent = new Intent(context,MyActivity.class);\n PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);\n\n Notification notification = new NotificationCompat.Builder(context)\n .setContentIntent(pendingIntent)\n .setSmallIcon(R.drawable.ic_stat_gcm)\n .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))\n .setTicker("\xd0\x9d\xd0\xbe\xd0\xb2\xd0\xbe\xd0\xb5 \xd1\x81\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd0\xb5")\n .setWhen(System.currentTimeMillis())\n .setAutoCancel(true)\n .setContentTitle("\xd0\xa1\xd0\xbe\xd0\xbe\xd0\xb1\xd1\x89\xd0\xb5\xd0\xbd\xd0\xb8\xd0\xb5")\n .setContentText(message)\n .setGroup(GROUP_KEY_GUEST)\n .build();\n notificationManager.notify(id++, notification);\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4437 次 |
| 最近记录: |