通知构建器说明不起作用

时间:2017-10-25 07:58:46

标签: android notifications

我正在使用NotificationBuilder提供的一些方法创建通知。

我正在使用真实设备对其进行测试,并且从以下代码开始,我使用--->标记的所有方法似乎都无法正常工作:

long[] vibratePattern = new long[] {
                500, 1000,
                500, 1000};


        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "my_channel01");
        notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        ---> notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.testProfilePic));
        notificationBuilder.setAutoCancel(true);
        ---> notificationBuilder.setColor(Color.parseColor("#aa0000"));
        ---> notificationBuilder.setVibrate(vibratePattern);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build()); 

所以,用语言来说,没有振动,没有大图标,也没有背景颜色。

1 个答案:

答案 0 :(得分:0)

尝试这样做:

private static long[] vibratePattern = {500, 500, 500, 500, 500, 500, 500, 500, 500};
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Notification notification = new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(context.getString(R.string.change_after_sync))
                .setStyle(new Notification.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse, context)))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setLargeIcon(Largeicon)
                .setContentIntent(pendingIntent)
                .setVibrate(vibratePattern)
                .setLights(Color.GREEN, 2000, 2000)
                .build();