繁体   English   中英

用于持久通知的前台服务

[英]foreground service for persistent notification

我有一个内置于 api 级别 22 的应用程序,现在我必须将其更新为 api 级别 26,并且最初构建的持久通知不再有效。 我已经尝试了 stackoverflow 中的多个代码,但对我来说没有成功。

我的代码:

    void setUpAsForeground(String text) {
        Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
                mainActivity, 0);
        // Build the notification object.
        mNotificationBuilder = new Notification.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.radio_icon_128px)
                .setTicker(text)
                .setWhen(0)
//                .setWhen(System.currentTimeMillis())
                .setContentTitle(getResources().getString(R.string.app_name) + text)
                .setContentText(mMusicProvider.getCurrentSongTitle())
                .setContentIntent(openMainActivity)
                .setOngoing(true);
        startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
//        broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
    }

任何建议将不胜感激。

从 API 26 开始,您必须为您的应用程序提供一个通知渠道,然后才会显示任何通知。 对于 Android 7 及更低版本,需要设置优先级。 有关更多信息和示例,请参阅此文档: https : //developer.android.com/training/notify-user/build-notification

在 Android 8.0 及更高版本上发送通知之前,您必须通过传递 NotificationChannel 的实例向系统注册您的应用程序的通知通道。如果您只需要从您的应用程序发出通知,那么您可以考虑使用此代码。

 public class makeNotification {
        private static final String ChannelId = "ChannelId";
        private static final String CHANNEL_ID = "cahhnel";
        private static final int NOTIFICATION_ID = 99;

        public static void makenotification(Context context) {
          Intent intent = new Intent(context,DemoVolleyActivity.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        CHANNEL_ID,
                        "Channel Name",
                        NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(mChannel);
            }
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
                    .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                    .setSmallIcon(R.drawable.ic_lock_black_24dp)
                    .setContentTitle("My Notification")
                    .setContentText("notification_body")
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                    && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
            }
            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
        }
    }

暂无
暂无

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

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