点击通知无法打开应用/活动 [英] Clicking notification not opening app/activity

查看:250
本文介绍了点击通知无法打开应用/活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打开通知时遇到麻烦.我已按照Android文档上的说明进行操作,但无济于事.它可以毫无问题地创建通知,但是单击它就可以将其关闭.

I am having trouble having a notification open the app. I've followed the instructions on the Android docs, but to no avail. It creates the notification no problem, but clicking on it just dismisses it.

请帮助!预先感谢!

为什么单击通知无法打开应用?

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

推荐答案

我今天早些时候也遇到了同样的问题,如果您使用的是kitkat,则必须更改为:

I faced this same problem earlier today, if you are using kitkat you will have to change to:

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

或者您可以将标志添加到您的接收器,或者将通知从XML通知中启动:

or you can add the flag to your receiver, or activity launched from the notification in XML:

 android:exported="true"

这篇关于点击通知无法打开应用/活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆