【发布时间】: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