繁体   English   中英

不创建新的通知前台android服务

[英]not create new notification foreground android service

问题一旦创建通知但其他通知不创建和更新最后通知。 我想收到新的通知

通话方式:

 private void NotificationLoop() {

  // one notification
        G.HANDLER.postDelayed(new Runnable() {
            @Override
            public void run() {
                populateNotification(1, "task id : " + 1, 20);
            }
        }, 2000);
 // two notification
        G.HANDLER.postDelayed(new Runnable() {
            @Override
            public void run() {
                populateNotification(2, "task id : " + 2, 40);
            }
        }, 2000);
// three notification
        G.HANDLER.postDelayed(new Runnable() {
            @Override
            public void run() {
                populateNotification(3, "task id : " + 3, 80);
            }
        }, 3000);


    }

populateNotification我的方法来自服务:

 private void populateNotification(int id, String title, int percent) {

        Notification notification;
        Intent CancelIntent = new Intent(this, ServiceDownload.class);
        CancelIntent.setAction(Constant.ACTION.CANCEL_DOANLOAD_ACTION);
        CancelIntent.putExtra("ID", id);
        PendingIntent CCancelIntent = PendingIntent.getService(this, 0, CancelIntent, 0);

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

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        notification = new NotificationCompat.Builder(this)
                .setContentTitle(title)
                .setTicker(title)
                .setContentText(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setAutoCancel(false)
                .setOngoing(false)
                .addAction(android.R.drawable.ic_menu_close_clear_cancel, this.getResources().getString(R.string.Cancel), CCancelIntent)
                .setProgress(100, percent, false)
                .setStyle((new NotificationCompat.BigTextStyle().bigText(title)))
                .build();
        startForeground(id, notification);
    }

前台服务只有一个通知。

所以当你调用startForeground(id, notification); 你只需要替换它。

你没有太多选择来解决你的问题

  1. 创建与服务无关的新通知
  2. 停止前台服务并再次启动它,因此通知将是全新的,但有可能,用户不会注意到它是新通知而不是更新旧通知。
  3. 通过新通知启动新的前台服务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM