This is an easy way to trigger a notification using the default vibration and sound from the system.
private void sendNotification(String message, String tick, String title, boolean sound, boolean vibrate, int iconID) { 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); Notification notification = new Notification(); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); if (sound) { notification.defaults |= Notification.DEFAULT_SOUND; } if (vibrate) { notification.defaults |= Notification.DEFAULT_VIBRATE; } notificationBuilder.setDefaults(notification.defaults); notificationBuilder.setSmallIcon(iconID) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setTicker(tick) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 , notificationBuilder.build()); }
Add vibration resolution if you intend to use it:
<uses-permission android:name="android.permission.VIBRATE"/>
Good luck. "
Maher Abuthraa May 05 '16 at 10:37 p.m. 2016-05-05 22:37
source share