更改前台通知

ou6hu8tu  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(361)

我有一个前台服务在我的应用程序与工作通知后台工作,但是,我想自定义我自己的通知。
这是默认的通知图片:https://ibb.co/tqrl9q8
我厌倦了更改通知:

@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
        startMyOwnForeground();
    else{
    startForeground(1, new Notification());
    }

}

对此:

@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
        startMyOwnForeground();
    else{
    Intent intent = new Intent(this, BackgroundLocation.class);

    // The PendingIntent that leads to a call to onStartCommand() in this service.
    PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // The PendingIntent to launch activity.
    PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch, getString(R.string.launch_activity), activityPendingIntent);
    NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.remove_location_updates), servicePendingIntent);

    Notification notification = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID)
            .setContentTitle("App is running in background")
            .addAction(action)
            .addAction(action2)
            .setPriority(NotificationManager.IMPORTANCE_HIGH)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setSmallIcon(R.drawable.letherknowtitle)
            .build();

    startForeground(1, notification);
    }

}

使用上述代码时,通知不会更改。但是,当用户sdk>android o时,通知正在更改。
谢谢你的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题