从通知中获取自定义操作

时间:2017-09-30 05:41:37

标签: android notifications

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

        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。我有什么想法可以捕获这个吗?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

从通知中获取操作列表(如果存在)
Notification n = sbn.getNotification();
for(Action action : n.actions){
    String title = action.title.toString;
    ...
}

您可以使用action-title创建一个按钮,然后编写代码onClick()以执行与通知相对应的操作。

更确切地说 - 你可以打电话

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

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