How to add button to notifications in android?

前端 未结 8 1432
孤独总比滥情好
孤独总比滥情好 2020-12-05 03:53

My app plays music and when users open notifications screen by swiping from the top of the screen ( or generally from the bottom right of the screen on tablets ), I want to

相关标签:
8条回答
  • 2020-12-05 04:23

    You can create an intent for the action (in this case stop playing)and that add it as an action button to your notification.

    Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
    snoozeIntent.setAction(ACTION_SNOOZE);
    snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
    PendingIntent snoozePendingIntent =
            PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                    snoozePendingIntent);
    

    Please refer to the Android documentation.

    0 讨论(0)
  • 2020-12-05 04:32

    tested, working code with android Pie. These all go inside the same service class.

    Show a notification:

    public void setNotification() {
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
        NotificationChannel channel = new NotificationChannel("a", "status", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("notifications");
        notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    
        }
    else
        notificationManager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    
    Receiver.service = this;
    Notification.MediaStyle style = new Notification.MediaStyle();
    
    notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Notification")
            .addAction(R.drawable.close_icon,  "quit_action", makePendingIntent("quit_action"))
            .setStyle(style);
    
    style.setShowActionsInCompactView(0);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
        notification.setChannelId("a");
        }
    
    // notificationManager.notify(123 , notification.build()); // pre-oreo
    startForeground(126, notification.getNotification());
    }
    

    Helper function:

    public PendingIntent makePendingIntent(String name)
        {
        Intent intent = new Intent(this, FloatingViewService.Receiver.class);
        intent.setAction(name);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        return pendingIntent;
        }
    

    To handle the actions:

    static public class Receiver extends BroadcastReceiver {
    static FloatingViewService service;
    
    @Override
    public void onReceive(Context context, Intent intent)
        {
    
        String whichAction = intent.getAction();
    
        switch (whichAction)
            {
    
            case "quit_action":
                service.stopForeground(true);
                service.stopSelf();
                return;
    
            }
    
        }
    }
    

    You'll need to update your manifest too:

    <receiver android:name=".FloatingViewService$Receiver">
            <intent-filter>
                <action android:name="quit_action" />
            </intent-filter>
        </receiver>
    
    0 讨论(0)
提交回复
热议问题