Ich habe den neueren Builder für NotificationCompat verwendet, und ich kann die Benachrichtigung nicht erhalten, um einen Ton zu erzeugen. Es vibriert und blinkt das Licht. In der Android-Dokumentation soll ein Stil festgelegt werden, mit dem ich gearbeitet habe:
builder.setStyle(new NotificationCompat.InboxStyle());
Aber kein Ton?
Der vollständige Code:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MenuScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
Was fehlte in meinem vorherigen Code:
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
Legen Sie Ihre Sounddatei einfach in den Res\raw\siren.mp3
-Ordner und verwenden Sie dann diesen Code:
Für benutzerdefinierten Sound:
Notification notification = builder.build();
notification.sound = Uri.parse("Android.resource://"
+ context.getPackageName() + "/" + R.raw.siren);
Für Standardton:
notification.defaults |= Notification.DEFAULT_SOUND;
Für benutzerdefinierte vibrieren:
long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
Für Standardvibrieren:
notification.defaults |= Notification.DEFAULT_VIBRATE;
Ein anderer Weg für den Standardton
builder.setDefaults(Notification.DEFAULT_SOUND);
USE Can Codeding
String en_alert, th_alert, en_title, th_title, id;
int noti_all, noti_1, noti_2, noti_3, noti_4 = 0, Langage;
class method
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
//
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
Geben Sie einfach den folgenden einfachen Code ein:
notification.sound = Uri.parse("Android.resource://"
+ context.getPackageName() + "/" + R.raw.sound_file);
Für Standardton:
notification.defaults |= Notification.DEFAULT_SOUND;
Sie müssen RingtoneManager verwenden
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://niravranpara.blogspot.com/";
Code für den Benachrichtigungsmanager mit Alarmton können Sie auch den Klingelton RingtoneManager.TYPE_RINGTONE einstellen
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse(myBlog));
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification note = new Notification(R.drawable.ic_launcher, "Alarm", System.currentTimeMillis());
note.setLatestEventInfo(getApplicationContext(), "Alarm", "sound" + " (alarm)", pi);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
note.sound = alarmSound;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(MY_NOTIFICATION_ID, note);
Sie müssen den Builder verwenden. setSound
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder.setSound(alarmSound);
manager.notify(1, builder.build());
Sie können eine Funktion erstellen:
public void playNotificationSound()
{
try
{
Uri alarmSound = `Uri.parse(ContentResolver.SCHEME_Android_RESOURCE + "://" + MyApplication.getInstance().getApplicationContext().getPackageName() + "/raw/notification");`
Ringtone r = RingtoneManager.getRingtone(MyApplication.getInstance().getApplicationContext(), alarmSound);
r.play();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Rufen Sie diese Funktion auf, wenn Sie eine Benachrichtigung erhalten.
Hier ist Raw der Ordner in res und die Benachrichtigung ist die Sounddatei im Raw-Ordner.
Auf Oreo (Android 8) und höher sollte dies auf diese Weise für benutzerdefinierten Sound (Benachrichtigungskanäle) erfolgen:
Uri soundUri = Uri.parse(
"Android.resource://" +
getApplicationContext().getPackageName() +
"/" +
R.raw.Push_sound_file);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
// Creating Channel
NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, audioAttributes);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
.createNotificationChannel(notificationChannel);
1. Legen Sie die Datei "yourmp3file" .mp3 in den Raw-Ordner (dh in den Res-Ordner).
2. in deinen Code stecken ..
Notification noti = new Notification.Builder(this)
.setSound(Uri.parse("Android.resource://" + v.getContext().getPackageName() + "/" + R.raw.yourmp3file))//*see note
Dies ist, was ich in mein onClick (View v) als nur "context (). GetPackageName ()" einsetze, wird von dort nicht funktionieren, da es keinen Kontext bekommt
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mit der Notification.builder-Klasseninstanz (Builder), die unten angegeben ist, können Sie den Standardton bei der Benachrichtigung wiedergeben:
builder.setDefaults(Notification.DEFAULT_SOUND);
private void showNotification() {
// intent triggered, you can add other intent for other actions
Intent i = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0);
//Notification sound
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = null;
if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES.JELLY_BEAN) {
mNotification = new Notification.Builder(this)
.setContentTitle("Wings-Traccar!")
.setContentText("You are punched-in for more than 10hrs!")
.setSmallIcon(R.drawable.wingslogo)
.setContentIntent(pIntent)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.addAction(R.drawable.favicon, "Goto App", pIntent)
.build();
}
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
// myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
rufen Sie diese Funktion auf, wo immer Sie möchten. das hat für mich funktioniert
In Android OREO oder einer späteren Version Nach Registrierung des Kanals beim System; Sie können die Wichtigkeit nicht ändern oder andere Benachrichtigungsverhalten nach diesem gleichen Kanal (Vor dem Deinstallieren der App)
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,audioAttributes);
Priorität ist auch hier am wichtigsten Benachrichtigung festlegen Priorität hoch durch Verwendung von
Vom Benutzer sichtbare Wichtigkeit (Android 8.0 und höher)
1) Dringend Gibt einen Ton aus und erscheint als Heads-Up-Benachrichtigung. -> IMPORTANCE_HIGH
2) High Gibt einen Ton aus -> IMPORTANCE_DEFAULT
3) Mittel Kein Ton -> IMPORTANCE_LOW
4) Niedrig Kein Ton und erscheint nicht in der Statusleiste -> IMPORTANCE_MIN
das gleiche funktioniert in der gleichen Reihenfolge Priorität (Android 7.1 und niedriger)
1) PRIORITY_HIGH oder PRIORITY_MAX
2) PRIORITY_DEFAULT
3) PRIORITY_LOW
4) PRIORITY_MIN