繁体   English   中英

android - 如何在单击通知时显示对​​话框

[英]android - How to show a dialog when click on notification

我通过BroadcastReceiver在我的应用程序中收到通知,问题是,如何将数据解析为活动并与接收到的数据进行对话?

这是我的代码但是当我点击通知时,没有任何反应:

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

        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse(link));
        PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        Notification myNotification = new NotificationCompat.Builder(ctx)
                .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setLargeIcon(remote_picture)
                .setContentTitle(onvan).setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg).setContentIntent(pending).build();
        notificationManager.notify(1, myNotification);

我有一些变量像链接,msg,onvan包含我的数据,我需要将这些变量发送到一个活动并进行对话。

我怎么能这样做?

您可以使用方法putExtra将通知中的数据发送到另一个Activity并将其放入

PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));

取而代之的

PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);`

尝试这个:

Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID, 
            notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

只需让PendingIntent打开你的一个Activity,让你的Activity完全透明,然后打开一个Dialog。

编辑:如果您想从通知点击事件中打开一个活动:

假设notif是您的Notification对象:

Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;

有关详细信息,请访问此处 Android - 触摸通知时提示对话框窗口

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM