1

我一直在通过 Anna Xu 的youtube 教程制作闹钟

她最终将她的代码保留在 If 语句中,它似乎可以点击并调出她的应用程序。我已经根据在 Pixel XL 上搜索 Oreo 的修复程序进行了修改。这是我到目前为止所拥有的:

// notification parameters
        int notifyID = 1;
        String CHANNEL_ID = "my channel_01";
        CharSequence name = "Hello";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        // set up the notification service
        NotificationManager notify_manager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        notify_manager.createNotificationChannel(mChannel);

        Notification notification_popup = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("An alarm is going off!")
                .setContentText("Click me!")
                .setChannelId(CHANNEL_ID).build();
        // set up notification start command
        notify_manager.notify(0, notification_popup);

这会在警报响起时弹出通知,但点击并不会弹出应用程序,实际上我认为您根本无法点击它,您只能将其滑开。我只想能够单击通知将应用程序带到前台。我是 Java 和 Android Studio 的超级新手,现在奥利奥系统闹钟不可信,这一切都是必要的提示。任何帮助都会很棒。

4

1 回答 1

0
 //Create pendingIntent to open your particular activity     
  Intent notificationIntent = new Intent(context, YourActivity.class);

            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);

            PendingIntent intent = PendingIntent.getActivity(context, 0,
                    notificationIntent, 0);
           //Then edit here 

           Notification notification_popup = new Notification.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("An alarm is going off!")
                    .setContentText("Click me!")
                    .setContentIntent(notificationIntent )
                    .setChannelId(CHANNEL_ID).build();
于 2018-01-08T06:18:58.317 回答