简体   繁体   中英

No vibration for setVibrate for android notifications

I have a method that create notifications. In that method I am looping a list. According to that list I am creating notification. Notifications are creating correctly but the problem is no vibration occurs for any notifications. Also light not working.

protected void displayNotification(ArrayList<OpenNotificationData> li) {

    for (int i = 0; i < li.size(); i++) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "id");

        mBuilder.setContentTitle("PM Notification");
        mBuilder.setContentText("You've received new message");
        mBuilder.setTicker("New Message Alert!");
        mBuilder.setAutoCancel(true);
        mBuilder.setSmallIcon(R.drawable.ceat_noti);
        mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
        mBuilder.setLights(Color.RED, 3000, 3000);
        /* Add Big View Specific Configuration */
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle("PM notification");

        // Moves events into the big view
        inboxStyle.addLine(li.get(i).getSection());
        inboxStyle.addLine(li.get(i).getMachine());
        inboxStyle.addLine(li.get(i).getBreakDwon());

        mBuilder.setStyle(inboxStyle);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.notify(i, mBuilder.build());
        }

    }

}

I also add AndroidManifest VIBRATE permission

In Your Code, you have added five values in new Long[] array in setVibrate() method. While the method will accept only four values of array

Just Replace these from your code:

mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }); 

to

mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000 });

And for the LED to blink, It will only blink when the device screen is off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM