I fire a notification from MainActivity
class. When user click the notification, i'd like to return back to MainActivity
class and execute a method. I'd also like to know which notification is clicked (Assuming that i fire multiple notifications with different id
). Here what i did and it didn't work
Inside MainActivity.class
:
private void showNotification(String title, String message, int id) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message);
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction("mAction");
PendingIntent resultPendingIntent = PendingIntent.getBroadcast(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
}
Same inside MainActivity.class
i create a BroadcastReceiver
class but it never got called:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("mAction")) {
//execute my method here
}
}
}
I did add MyBroadcastReceiver.class
receiver in AndroidManifest.xml
:
<receiver android:name=".MyBroadcastReceiver" > </receiver>