Event OnClick for a button in a custom notification

后端 未结 5 1605
再見小時候
再見小時候 2020-11-27 15:53

I have a custom notification with a button. To set the notification and use the event OnClick on my button I\'ve used this code:

//Notification and intent of         


        
5条回答
  •  离开以前
    2020-11-27 16:43

    You need to create Service to detect on Click event: For example Create NotificationIntentService.class and put below code:

    public class NotificationIntentService extends IntentService {
    
        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         */
        public NotificationIntentService() {
            super("notificationIntentService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            switch (intent.getAction()) {
                case "left":
                    android.os.Handler leftHandler = new android.os.Handler(Looper.getMainLooper());
                    leftHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getBaseContext(),
                                    "You clicked the left button", Toast.LENGTH_LONG).show();
                        }
                    });
                    break;
                case "right":
                    android.os.Handler rightHandler = new android.os.Handler(Looper.getMainLooper());
                    rightHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getBaseContext(), "You clicked the right button", Toast.LENGTH_LONG).show();
                        }
                    });
                    break;
            }
        }
    }
    

    Add this metod to your activity:

    private void sendNotification() {
    
        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
        expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
        expandedView.setTextViewText(R.id.notification_message, mEditText.getText());
        // adding action to left button
        Intent leftIntent = new Intent(this, NotificationIntentService.class);
        leftIntent.setAction("left");
        expandedView.setOnClickPendingIntent(R.id.left_button, PendingIntent.getService(this, 0, leftIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        // adding action to right button
        Intent rightIntent = new Intent(this, NotificationIntentService.class);
        rightIntent.setAction("right");
        expandedView.setOnClickPendingIntent(R.id.right_button, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    
        RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
        collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // these are the three things a NotificationCompat.Builder object requires at a minimum
                .setSmallIcon(R.drawable.ic_pawprint)
                .setContentTitle(NOTIFICATION_TITLE)
                .setContentText(CONTENT_TEXT)
                // notification will be dismissed when tapped
                .setAutoCancel(true)
                // tapping notification will open MainActivity
                .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
                // setting the custom collapsed and expanded views
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                // setting style to DecoratedCustomViewStyle() is necessary for custom views to display
                .setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle());
    
        // retrieves android.app.NotificationManager
        NotificationManager notificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
    

提交回复
热议问题