有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用于持久通知的java前台服务

我有一个应用程序是在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) 个答案

  1. # 1 楼答案

    在Android 8.0及更高版本上发送通知之前,必须通过传递NotificationChannel实例向系统注册应用程序的通知频道 如果你只需要从你的应用程序中做一个通知,你就可以考虑使用这个代码。p>

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