Notification action button not clickable in lock screen

后端 未结 2 1648
栀梦
栀梦 2020-12-19 15:13

In order to better support Android 5 notifications, I am now setting my app\'s notification visibilty to \"public\". After considering the answers on Lollipop Notification s

2条回答
  •  伪装坚强ぢ
    2020-12-19 16:01

    Instead of adding an action, define your own notification layout and connect a pendingIntent to fire to that via RemoteView. (The example below is based on the Holo look and feel and would still need to updated for lollipop. You can find all te correct resources in the android-21/data/res folder of your sdk)

    // NOTE: while creating pendingIntent: requestcode must be different!
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(myService)
            .setSmallIcon(R.drawable.notification_icon).setContentTitle("My Title")
            .setContentText("Service running in the background");
    Intent openIntent = new Intent(MainActivity.this, MainActivity.class);
    PendingIntent pOpenIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
    mBuilder.setContentIntent(pOpenIntent);
    
    // Notification with exit button if supported
    String ACTION_NOTIFICATION_EXITACTIVITY = "com.jmols.example.exitactivity";
    Intent exitIntent = new Intent();
    exitIntent.setAction(ACTION_NOTIFICATION_EXITACTIVITY);
    PendingIntent pExitIntent = PendingIntent.getBroadcast(this, 1, exitIntent, 0);
    RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification_discoveryservice);
    view.setOnClickPendingIntent(R.id.notification_closebtn_ib, pExitIntent);
    mBuilder.setContent(view);
    

    With a notification layout:

    
    
    
        
    
        
    
        
    
            
    
            
    
        
    
    
    

    And the notification background is:

    
    
        
        
    
    

    notification_bg_normal.9 notification_bg_normal_pressed.9

    And the notification imagebutton background is:

    
    
        
        
    
    

    notification_imagebtn_bg_normal.9.png notification_imagebtn_bg_normal_pressed.9.png

提交回复
热议问题