EGHDK
EGHDK

Reputation: 18120

Notification onClick

I have successfully created a notification thanks to my other question, using NotificationCompat. I want to now just change what the notification does onClick. I'd really just like to have an alert dialog pop up (I've seen some app's do it) but everytime I click it, I just have my activity show up. Any ideas?

The notification code:

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

Upvotes: 3

Views: 720

Answers (3)

Pong Petrung
Pong Petrung

Reputation: 1447

you can set

 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);


    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.BigPictureStyle notiStyle = new
            NotificationCompat.BigPictureStyle();

    android.app.Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(en_title)
            .setContentText(en_alert)
            .setAutoCancel(true)
            .setNumber(++numMessages)
            .setStyle(notiStyle)

            //.setContentIntent(resultPendingIntent)

                   .setContentIntent(pendingIntent)

            .build();

    notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    notificationManager.notify(1000, notification); 

Upvotes: 0

coredump
coredump

Reputation: 734

You can probably use Remote Views to show a dialog without going to your app process.

Upvotes: 0

kabuko
kabuko

Reputation: 36302

You can't have a normal dialog without an activity. There are several possible workarounds though including styling the activity like a dialog and making the activity itself invisible and launching a dialog from it immediately.

Upvotes: 2

Related Questions