3

我有这个用于通知生成的功能。我也根据奥利奥的要求更新了代码。这里可能是什么问题。任何帮助表示赞赏。它适用于所有其他手机。我也重新检查了通知设置

private void sendNotification(String messageTitle,String messageBody) {
        android.net.Uri sound;
        Intent intent = new Intent(getActivity(), MainActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0 /* request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long[] pattern = {500, 500, 500, 500, 500};
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        SharedPreferences demoprefs = getActivity().getSharedPreferences("demo", MODE_PRIVATE);
        demiIsLoggedIn = demoprefs.getBoolean("demo", true);
        if (!demiIsLoggedIn)
            sound = defaultSoundUri;
        else
            sound = null;
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getActivity())
                .setSmallIcon(getActivity().getApplicationInfo().icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE, 1, 1)
                .setSound(sound)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = "my_channel_01";// The id of the channel.
            CharSequence name = "notification channel";// The user-visible name of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationManager.createNotificationChannel(mChannel);
        }
        notificationManager.notify(12 /* ID of notification */, notificationBuilder.build());
        Log.d("wajdan","oneplus");

    }
4

1 回答 1

1

尝试NotificationChannel在 notificationBuilder中设置

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity().getApplicationContext(), "my_channel_01")
...
于 2018-03-22T08:30:39.207 回答