armin.mohammadi
armin.mohammadi

Reputation: 75

start activity by clicking notification android

i have problem with starting my activity when click on notification...

public class MyNotification extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "notification", 25).show();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Test Notification")
            .setContentText("This is test notification ");
    Intent myIntent = new Intent(context, MainActivity.class);
    PendingIntent intent2 = PendingIntent.getBroadcast(context, 1,
            myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(intent2);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1, notificationBuilder.build());
}

}

whats wrong ???

Upvotes: 0

Views: 9125

Answers (1)

M D
M D

Reputation: 47817

Try this way

 PendingIntent intent2 = PendingIntent.getActivity(context, 1,
        myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

instead of PendingIntent.getActivity use PendingIntent.getBroadcast

 PendingIntent intent2 = PendingIntent.getBroadcast(context, 1,
        myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

For more info go to http://android-er.blogspot.in/2013/06/start-activity-once-notification-clicked.html

Upvotes: 5

Related Questions