我的通知只是在虚拟设备上工作,而不是在真实的手机上工作。

问题描述 投票:0回答:1

通知在虚拟设备上做得很好,但在真实的手机上就不行了。我需要有任何用户权限吗?我不知道如何解决这个问题 :D

以下是我的代码

NotificationManager notificationM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(whitealarm)
            .setContentTitle(acti.name)
            .setContentText(interval.timeString())
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setOnlyAlertOnce(true)
            .setAutoCancel(true);

    if(!(audioNotiName.equals("none") ||audioNotiName.equals(""))){
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/raw/"+audioNotiName);
        builder.setSound(uri);

    }
    int notificationid=113;

    if(notiHaveVibration){
        final Vibrator v = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));

        } else {
            v.vibrate(1000);
        }
    }
    notificationM.notify(notificationid,builder.build());
java android
1个回答
1
投票

在真实的手机上,你必须检查安卓版本.从奥利奥,它改变了。所以你必须添加这样的逻辑。

        // create Channel for over oreo
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel("channel", "channel_nm", NotificationManager.IMPORTANCE_HIGH);
            mChannel.setVibrationPattern(new long[]{500, 500, 500, 500});
            mChannel.setLightColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build();
            mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            mChannel.enableVibration(true);
            mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), att);
            notificationManager.createNotificationChannel(mChannel);
            notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mChannel.getId());
        } else {
            notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
        }
        notificationBuilder
                .setLargeIcon(icon2)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(pushTitle)
                .setContentText(contentText)
                .setStyle(bigText)
                .setLights(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), 3000, 3000)
                .setVibrate(new long[]{500, 500, 500, 500})
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent);


        int random_num = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(random_num, notificationBuilder.build());
© www.soinside.com 2019 - 2024. All rights reserved.