简体   繁体   English

点击通知无法打开应用/活动

[英]Clicking notification not opening app/activity

I am having trouble having a notification open the app. 我在收到通知时无法打开应用程序。 I've followed the instructions on the Android docs, but to no avail. 我已按照Android文档上的说明进行操作,但无济于事。 It creates the notification no problem, but clicking on it just dismisses it. 它可以毫无问题地创建通知,但是单击它就可以将其关闭。

Please help! 请帮忙! Thanks in advance! 提前致谢!

Why is clicking the notification not opening the app? 为什么单击通知无法打开应用程序?

Intent intent = new Intent(this, MainActivity.class);

    String type = "";
    if (extras.containsKey(KEY_TYPE)) type = extras.getString(KEY_TYPE);

    String text = "";

    if (type.equalsIgnoreCase(TYPE_MATCH_FOUND)) {
        // TODO: send intent with all variables, trigger matched fragment when user goes into app



        text = getResources().getString(R.string.msg_found_match);

        intent.putExtra(KEY_TYPE, TYPE_MATCH_FOUND);
    }
    else if (type.equalsIgnoreCase(TYPE_MESSAGE)) {
        // TODO: trigger chat fragment when user goes into app

        text = getResources().getString(R.string.msg_new_message_from);

        intent.putExtra(KEY_TYPE, TYPE_MESSAGE);
    }

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);        

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("LFDate")
        .setContentText(text)
        .setAutoCancel(true)
        .setLights(Color.parseColor("#0086dd"), 2000, 2000);

    if (prefs.getNotificationVibrate()) mBuilder.setVibrate(new long[] {1000, 1000, 1000});
    if (prefs.getNotificationSound()) mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);

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

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

I faced this same problem earlier today, if you are using kitkat you will have to change to: 我今天早些时候遇到了同样的问题,如果您使用的是kitkat,则必须更改为:

// Have pending intent use FLAG_CANCEL_CURRENT, cause on 
// kitkat you get a permission denied error
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

or you can add the flag to your receiver, or activity launched from the notification in XML: 或者,您可以将标志添加到您的接收器中,或者将通知中的活动添加为XML:

 android:exported="true"

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

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