持续性通知的前台服务

时间:2018-11-09 13:39:37

标签: java android api deprecated

我有一个在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);
    }

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在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());
        }
    }