为什么在从服务通知调用时未决意图不启动 activity

Why a pending intent does not start an activity when called form a service's notification

在我的 Github 帐户上,我做了一个简单的项目来描述我的问题 here my project

我的 Main Activity 有一个按钮,其 onClick 方法调用后台服务。 后台服务等待 5 秒,然后触发通知。

这里是 BackgroundService 的代码(你可以在 Github

@Override
    protected void onHandleIntent(Intent intent) {//methode appelée en arrière plan
        if (intent != null) {

            try{
                Thread.sleep(5000);
            } catch (Exception e){
                e.printStackTrace();
            }

            mNotificationsManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            CharSequence tickerText = "view hike";
            Intent intentReceiverAcctivity = new Intent(TAG_INTENT);
            intentReceiverAcctivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intentReceiverAcctivity, 0);

            Notification.Builder builder = new Notification.Builder(this);

            builder.setAutoCancel(false);
            builder.setTicker(tickerText);
            builder.setContentTitle("The Hike");
            builder.setContentText("HikeMap is available");
            builder.setSmallIcon(R.drawable.explore);
            Bitmap large_icon_bmp = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.ic_action_map);
            builder.setLargeIcon(large_icon_bmp);
            builder.setContentIntent(pendingIntent);
            builder.setOngoing(true);
            builder.setSubText("Click on this icon to access the Hike's map");   //API level 16
            builder.setNumber(5000); //durée d'apparition de l'icone dans la barre de notifications ?
            builder.build();

            Notification notification = builder.getNotification();
            mNotificationsManager.notify(11, notification);

        }
    }

展开通知栏并单击 large_icon 应该启动第二个 Activity 调用 ReceiverActivity 其唯一目的是显示一个不同的消息。

在模拟器上,小图标出现在通知栏上,当我展开那个栏时,我可以看到大图标,但点击它不会触发 ReceiverActivity

在我的智能手机上,通知栏上没有小图标,扩展通知区域上没有大图标...

知道服务代码出了什么问题吗? 这个小例子的完整代码可以在 GitHub...

上找到

在此先感谢您的帮助!!!

Intent intentReceiverAcctivity = new Intent(TAG_INTENT);

public final static String TAG_INTENT = "NotificationClicked";

抱歉,您没有将 activity 设置为 运行。您应该使用带有两个参数的 Intent,例如

Intent intent = new Intent(Context, YourActivity.class);

这里是 NotificationChannel

的例子
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
                "channel_id",
                "channel_name",
                NotificationManager.IMPORTANCE_DEFAULT
        );
        channel.setDescription("channel_description");
        channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(channel);
        builder = new NotificationCompat.Builder(context, "channel_id");
    } else {
        builder = new NotificationCompat.Builder(context);
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    }