2

以下のコードは、Android O verison以下のすべてのデバイスで動作します。アンドロイドOの場合、 addAction()メソッドが機能しない、つまりボタンのクリックがアンドロイドOで機能しない場合。
助けてください。私は、Androidのオレオであなたはそれ明示的なテント(マニフェストに受信機を置くと十分ではありません、実際には、それはそれに注意を払っています)ようにする必要があり、同じに走ったNotification.addActionがAndroidで動作しないO

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

      Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY"); 
      mediaPlayerReceiver.putExtra("NewsArticle", news); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaPlayerReceiver, PendingIntent.FLAG_UPDATE_CURRENT); 

      Intent dismissNotification = new Intent("com.consult.news.receiver.DISMISS"); 
      dismissNotification.putExtra("Notification_ID", 1); 
      PendingIntent dismissNotificationIntent = PendingIntent.getBroadcast(context, 0, dismissNotification, PendingIntent.FLAG_UPDATE_CURRENT); 

      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
       String CHANNEL_ID = "my_channel_01"; 
       String CHANNEL_NAME = "my Channel Name"; 

       NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); 
       notificationChannel.enableLights(true); 
       notificationChannel.setLightColor(Color.RED); 
       notificationChannel.setShowBadge(true); 
       notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); 
       notificationManager.createNotificationChannel(notificationChannel); 
      } 

      NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01") 
        .setSmallIcon(R.drawable.ic_notification_white) 
        .setColor(ContextCompat.getColor(context, R.color.accent)) 
        .setContentTitle(context.getString(R.string.Consult_Univadis_Title)) 
        .setStyle(new NotificationCompat.BigTextStyle().bigText(news)) 
        .addAction(isPlaying ? R.drawable.ic_notification_white : R.drawable.ic_notification_white, isPlaying ? "Play" : "Pause", pendingIntent) 
        .addAction(R.drawable.ic_notification_white, "Close", dismissNotificationIntent) 
        .setOngoing(true) 
        .setAutoCancel(false); 

      notificationManager.notify(1, builder.build()); 
+1

チェックアウトこの:https://stackoverflow.com/questions/35647821/android-notification-addaction-deprecated-in-api-23 –

+0

@HareshChhelanaは - 私はそれも試してみましたが、それはそれはだ – Ragini

答えて

4

あなたが意図を作る際に、setClassメソッドを使用して、それが明示的に行います。

「これは」コンテキストとYourReceiverで、あなたが行動に耳を傾けることを期待されているレシーバのクラスがある
Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY"); 
mediaPlayerReceiver.putExtra("NewsArticle", news); 
mediaPlayerReceiver.setClass(this, YourReceiver.class); 

あなたはこれがあなたのために働いていた場合、私に教えてくださいdismissNotification意図

のために同じことを行う必要があります。

+0

おかげアンドロイドOで働いていませんOreoでは通知ボタンのアイコンが表示されないのはなぜですか。あなたは何か考えていますか? – Ragini

+1

@RaginiあなたはNougat以降で表示するアイコンを取得しません Android Nougatでデザインが変更されました。 addAction(Notification.Actionアクション)によって定義されたアイコンは、デバイスによってもう表示されません。古いデバイスやAndroid Wearデバイスにはまだ必要です。 https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html 「あなたはアイコンが代わりに通知シェードの拘束されたスペースにラベル自体のためのスペースが設けられていますが、通知アクションアイコンはまだ必要です –

関連する問題