【问题标题】:Getting custom actions from notification从通知中获取自定义操作
【发布时间】:2017-09-30 05:41:37
【问题描述】:

我使用的服务会自动向我发送推送通知,并且我想自动按下这些通知的确认选项。我可以使用以下代码捕获通知:

        import android.app.Notification;
        import android.app.PendingIntent;
        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.os.Bundle;
        import android.service.notification.NotificationListenerService;
        import android.service.notification.StatusBarNotification;
        import android.util.Log;
        import android.support.v4.content.LocalBroadcastManager;

        import java.io.ByteArrayOutputStream;

public class NotificationHandler extends NotificationListenerService {

    Context context;

    @Override

    public void onCreate() {
        Log.e("STATE","HERE AGAIN");

        super.onCreate();
        context = getApplicationContext();

    }

    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {
        String pack = sbn.getPackageName();
        String ticker = "";
        if (sbn.getNotification().tickerText != null) {
            ticker = sbn.getNotification().tickerText.toString();
        }
        Bundle extras = sbn.getNotification().extras;
//        try {
//            //sbn.getNotification().contentIntent.send();
//        } catch (PendingIntent.CanceledException e) {
//            e.printStackTrace();
//        }
        String title = extras.getString("android.title");
        String text = extras.getCharSequence("android.text").toString();
        int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
        Bitmap id = sbn.getNotification().largeIcon;

        Log.i("NEW", "----------");
        Log.i("Package", pack);
        Log.i("Ticker", ticker);
        Log.i("Title", title);
        Log.i("Text", text);
        sbn.getNotification().contentIntent.sen
        for (String key : bundle.keySet()) {
            Log.i("Intent", key);
        }
        Log.i("NEW", "----------");


        Intent msgrcv = new Intent("Msg");
        msgrcv.putExtra("package", pack);
        msgrcv.putExtra("ticker", ticker);
        msgrcv.putExtra("title", title);
        msgrcv.putExtra("text", text);
        if (id != null) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            id.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            msgrcv.putExtra("icon", byteArray);
        }
        LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg", "Notification Removed");

    }
}

我无法弄清楚如何捕获用于确认通知然后单击它的自定义选项。确认/拒绝按钮看起来像similar to the buttons on this notification。有什么想法可以捕捉到这一点吗?

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    您可以使用以下代码从通知中获取操作列表(如果存在)

    Notification n = sbn.getNotification();
    for(Action action : n.actions){
        String title = action.title.toString;
        ...
    }
    

    您可以创建一个带有动作标题的按钮,然后编写代码onClick()来执行与通知对应的动作。

    更准确地说,你可以调用

    action.actionIntent.send(); // or another variation of send() 
    

    从按钮上的 onClick() 执行与发送通知的应用程序提供的 pendingIntent 对应的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多