2016年8月11日 星期四

how to make a notification have sound?

47 how to make a notification have sound?
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);


if not set ,no sound


Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

       NotificationCompat.Builder mBuilder =
               new NotificationCompat.Builder(context)
                       .setSmallIcon(R.mipmap.ic_launcher)
                       .setContentTitle("Tabata Timer")
                       .setContentText("It is time to do tabata")
                       .setAutoCancel(true)
                       .setSound(alarmSound);
// Creates an explicit intent for an Activity in your app
       Intent resultIntent = new Intent(context, MainActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
//        stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
       stackBuilder.addNextIntent(resultIntent);
       PendingIntent resultPendingIntent =
               stackBuilder.getPendingIntent(
                       0,
                       PendingIntent.FLAG_UPDATE_CURRENT
               );
       mBuilder.setContentIntent(resultPendingIntent);
       NotificationManager mNotificationManager =
               (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
       mNotificationManager.notify(0, mBuilder.build());