Android 当应用程序关闭(根本不运行)时,如何在Oreo中显示应用程序徽章?
所有方法调用都按预期执行,在通知栏中获取通知,但当应用程序关闭时,Appbadge不会显示在Android O设备中。请告诉我我犯了什么错误Android 当应用程序关闭(根本不运行)时,如何在Oreo中显示应用程序徽章?,android,Android,所有方法调用都按预期执行,在通知栏中获取通知,但当应用程序关闭时,Appbadge不会显示在Android O设备中。请告诉我我犯了什么错误 若我把频道创建代码和通知代码放在某个视图的侧击事件中,那个么当应用程序打开时,它可以正常工作 在安卓系统中,通知徽章不像在iOS中那样有效。它们只是显示应用程序在状态栏中有一些通知。当从状态栏中删除通知时,徽章也将消失。Hi@God,非常感谢您的快速回复。你说得对。在从状态栏中丢弃通知后,徽章计数也会消失,那么在android中支持应用程序徽章有什么意义呢
若我把频道创建代码和通知代码放在某个视图的侧击事件中,那个么当应用程序打开时,它可以正常工作 在安卓系统中,通知徽章不像在iOS中那样有效。它们只是显示应用程序在状态栏中有一些通知。当从状态栏中删除通知时,徽章也将消失。Hi@God,非常感谢您的快速回复。你说得对。在从状态栏中丢弃通知后,徽章计数也会消失,那么在android中支持应用程序徽章有什么意义呢(在这个用例中,它是多余的,用户可以在通知栏中看到相同的东西)。目前,我正在更新徽章计数,同时关闭应用程序,并发出虚拟通知。我见过一些像facebook这样的应用程序,当我们在读了几条消息后关闭应用程序时,他们会在没有显示任何通知的情况下更新应用程序徽章中的未读计数。如果有人可以的话,请尽快帮我?谢谢。在安卓系统中,我认为我们不支持在不向频道发布通知的情况下更新应用程序徽章数量。我找到的唯一方法是,使用builder.setNumber(未读)在MainActivity的side onPause()中发布通知。如果您希望应用程序徽章计数不应消失,则您已设置builder.setContinuous(true),其余部分可根据您的要求决定。
public class GcmBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{// Control comes here when any new notification is received
final Notification notification = getNotification(threadId, jobTitle, body, subTitle, title, imageURL);
EventLoggerService.showAppBadge(context, notification, new EventLoggerService.EventLoggerServiceStartedListner(){
@Override
public void eventLoggerServiceStarted() {
notificationManager.notify(101, notification);
}
});
}
public Notification getNotification(final String threadId, String jobTitle, final String body ,String subTitle, String title, final String imageURL)
{
final Context context = CareSDKApplication.singleton().getApplicationContext();
mThreadId = threadId;
mImageURL = imageURL;
mBody = body;
mTitle = title;
mSubTitle = subTitle;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationsUtils.NOTIFICATION_CHANNEL_ID)
// .setLargeIcon(largeIcon)
.setSmallIcon(R.drawable.message_notification)
.setAutoCancel(true)
.setContentIntent(getContentIntent(threadId))
.setTicker(getTicker(threadId));
RemoteViews customNotification = new RemoteViews(context.getPackageName(), R.layout.image_notification_custom_view);
builder.setCustomContentView(customNotification);
if (!TextUtils.isEmpty(title))
{
customNotification.setTextViewText(R.id.title, StringEscapeUtils.unescapeJava(title));
customNotification.setTextViewText(R.id.sub_title, StringEscapeUtils.unescapeJava(subTitle));
customNotification.setTextViewText(R.id.content, StringEscapeUtils.unescapeJava(body));
}
else
{
customNotification.setTextViewText(R.id.title, "");
customNotification.setTextViewText(R.id.sub_title, StringEscapeUtils.unescapeJava(body));
customNotification.setTextViewText(R.id.content, "");
}
customNotification.setTextViewText(R.id.time, new SimpleDateFormat("HH:mm").format(new Date()));
customNotification.setTextViewText(R.id.number_of_notifications, getUnreadCount(threadId) + "");
//builder.setPriority(Notification.PRIORITY_MAX);
mBuilder = builder;
// For versions equal and higher than jelly bean
if (Build.VERSION.SDK_INT >= 16) {
builder.setStyle(getStyle(threadId));
}
if (!TextUtils.isEmpty(imageURL)) {
mBigPicture = Utils.getImage(context, imageURL, this);
if (mBigPicture != null) {
setImageNotification(builder, context, threadId, body, title, subTitle);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setColor(0x00bce4);
}
Notification notification = builder.build();
return notification;
}
}
public class EventLoggerService extends IntentService
{
private static EventLoggerServiceStartedListner
mEventLoggerServiceStartedListner;
private static Notification mNotification;
@Override
public void onCreate()
{
super.onCreate();
// This is required for Android O
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1)
{
android.os.Debug.waitForDebugger();
NotificationsUtils notificationsUtils = new NotificationsUtils(this);
startForeground(1, mNotification);
Log.d("EventLoggerService", "EventLoggerService Created");
}
}
public interface EventLoggerServiceStartedListner{
public void eventLoggerServiceStarted();
}
@Override
public void onStart(@Nullable Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("EventLoggerService", "EventLoggerService Started");
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("EventLoggerService", "EventLoggerService onStartCommand");
mEventLoggerServiceStartedListner.eventLoggerServiceStarted();
return START_STICKY;
}
public static void showAppBadge(Context context, Notification notification, EventLoggerServiceStartedListner eventLoggerServiceStartedListner)
{
android.os.Debug.waitForDebugger();
mNotification = notification;
mEventLoggerServiceStartedListner = eventLoggerServiceStartedListner;
Intent service = new Intent(context, EventLoggerService.class);
context.startForegroundService(service);
}
}
public class NotificationsUtils extends ContextWrapper{
public static final String NOTIFICATION_CHANNEL_ID = "Care_push_note";
public static final String NOTIFICATION_CHANNEL_NAME = "Care.com";
Context mContext;
NotificationManager mNotificationManager;
public NotificationsUtils(Context context) {
super(context);
mContext = context;
createChannels();
}
public void createChannels()
{
// Notification Channel is created for same channel id as Notification is created in side getNotification method of GcmBroadcastReceiver
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLightColor(Color.GREEN);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mNotificationManager;
}
}