通知onClick

时间:2012-10-12 00:32:31

标签: java android android-notifications

我已使用NotificationCompat成功创建了通知,感谢我的其他question。我现在想改变通知对onClick的作用。我真的只想弹出一个警告对话框(我看过一些应用程序会这样做)但每次点击它时,我都会显示我的活动。有什么想法吗?

通知代码:

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

3 个答案:

答案 0 :(得分:2)

如果没有活动,您将无法进行正常对话。有几种可能的解决方法,包括styling the activity like a dialog,并使活动本身不可见,并立即从中启动对话框。

答案 1 :(得分:0)

您可以使用Remote Views显示对话框而无需进入应用程序。

答案 2 :(得分:0)

你可以设置

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