循环设置通知,直到用户单击

时间:2018-09-27 10:44:19

标签: android push-notification fcmp

这是我的通知代码:

   private void APP_FOREGROUND_showNotificationMessage(Context context, String title, String message, Intent intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    String channelId = "CHANNEL_ID";
    String channelName = "CHANNEL NAME";

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel mChannel = new NotificationChannel(channelId,
                channelName,
                NotificationManager.IMPORTANCE_HIGH);

        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();



        mChannel.setDescription(title);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic), attributes); // Here you set the sound


        if (manager != null)
            manager.createNotificationChannel(mChannel);
     }
     NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.vb_grey)
            .setContentTitle(title)
            .setColor(Color.RED)
            .setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic))
            .setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);

    Notification notification = builder.build();
    notification.flags = Notification.FLAG_INSISTENT;

    manager.notify(0, builder.build());
}

这是我的通知代码。

我已使用.setDefaults(Notification.FLAG_INSISTENT)进行循环播放,但通知声音仅响一次。

1 个答案:

答案 0 :(得分:0)

您不能使用setDefaults()

来设置标志

您可以使用以下代码:

NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.vb_grey)
            .setContentTitle(title)
            .setColor(Color.RED)
            .setOngoing(true)  //<-- you also need this one
            .setSound(Uri.parse("android.resource://"+context+context.getPackageName()+"/"+R.raw.offic))
            .setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);

Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;

还要检查this stackoverflow question