单击“大视图通知”不会启动活动

时间:2014-11-12 19:53:55

标签: android android-notifications

点击通知不会启动任何活动。我按照开发人员的代码,但有些东西不起作用。有什么建议吗?

    Intent dismissIntent = new Intent(getActivity(), Activity1.class);
    dismissIntent.setAction("action1");
    PendingIntent piDismiss = PendingIntent.getService(getActivity(), 0, dismissIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent notifyIntent = new Intent(getActivity(), Activity2.class);
    notifyIntent.setAction("action2");
    PendingIntent piNotify = PendingIntent.getService(getActivity(), 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(getActivity())
                    .setSmallIcon(R.drawable.app_icon)
                    .setContentTitle("Alert")
                    .setContentText("Alert text")
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText("Big text message"))
                    .addAction(R.drawable.icon1,
                            "Dismiss", piDismiss)
                    .addAction(R.drawable.icon2,
                            "Notify", piNotify);

    NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());

我还在“清单”中的活动中添加了android:exported="true"

1 个答案:

答案 0 :(得分:2)

我认为问题在于您正在致电

PendingIntent.getService()

而不是

PendingIntent.getActivity()

正如doc所说:

PendingIntent.getService : 检索将启动服务的PendingIntent,例如调用Context.startService()。给予服务的起始参数将来自Intent的附加内容。

PendingIntent getActivity :检索将启动新活动的PendingIntent,例如调用Context.startActivity(Intent)。请注意,活动将在现有活动的上下文之外启动,因此您必须在Intent中使用Intent.FLAG_ACTIVITY_NEW_TASK启动标记。