【问题标题】:foreground service for persistent notification用于持久通知的前台服务
【发布时间】:2018-11-09 13:39:36
【问题描述】:

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

任何建议将不胜感激。

【问题讨论】:

  • 你能描述一下“不工作”吗?
  • @LaurenzAlbe 是的,当我运行应用程序时它根本没有显示通知。

标签: java android api deprecated


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2019-08-05
      • 2012-01-21
      相关资源
      最近更新 更多