【问题标题】:Clicking notification not opening app/activity单击通知未打开应用程序/活动
【发布时间】:2014-07-09 23:54:22
【问题描述】:

我无法让通知打开应用程序。我已按照 Android 文档上的说明进行操作,但无济于事。它创建通知没有问题,但单击它只会关闭它。

请帮忙!提前致谢!

为什么点击通知没有打开应用?

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

【问题讨论】:

    标签: android android-intent push-notification android-pendingintent


    【解决方案1】:

    我今天早些时候遇到了同样的问题,如果您使用的是 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());
    

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

     android:exported="true"
    

    【讨论】:

    • 没问题!恰好我今天有幸经历了这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多