Ist es möglich, eine Benachrichtigung einen Broadcast-Empfänger zu starten?android Benachrichtigung löst nicht BroadcastReceivers onReceive
Ich habe diesen Code versucht, aber es funktioniert nicht.
Benachrichtigung wird erstellt, aber wenn ich darauf klicke passiert nichts.
HINWEIS: Wenn ich die notificationIntent von MyBroadcastReceiver.class auf eine Aktivität (wie MainActivity.class) zeigen, funktioniert es einwandfrei.
Meldungserstellung:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
int notificationIconId = XXXXXX
Notification notification = new Notification(
notificationIconId,
XXXXXX,
System.currentTimeMillis()
);
CharSequence contentTitle = XXXXXXX
CharSequence contentText = XXXXXX
Intent notificationIntent = new Intent(context,MyBroadcastReceiver.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(1,notification);
Hier ist die BroadcastReceiver
public static class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*
*/
}
}
Innen AndroidManifest.xml
<receiver android:name=".MyBroadcastReceiver" />
@Abhishek: Kein Problem. Als ich zum ersten Mal mit Benachrichtigungen arbeitete, fiel ich in die gleiche Falle, indem ich 'getActivity' für einen 'PendingIntent' verwendete, als ich eigentlich einen' Service' starten wollte (der 'getService' verwendet). Eine gut erlernte Lektion. :) – Squonk
@Abhishek hast du vergessen, setClass für die notificationIntent 'notificationIntent.setClass (context, MyBroadcastReceiver.class);' – AbdullahDiaa