向 Android 个自定义通知添加操作
Adding an action to Android custom notifications
我已阅读有关通知的 Android 开发人员指南,并且我创建了一个,如下所示:
private static void initNotificationCompatObject()
{
mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon).setContentTitle("My notification")
.setContentText("Test!");
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
}
通知本身已显示并有效。
我想做的是在按下通知后添加另一个动作。我想在我的 activity 中读取通知 ID(或在构建器的某种动作侦听器中)并根据 ID 执行某些操作。怎样才能实现这样的目标?
public static final int NOTIFICATION_ID_FIRST = 1000;
public static final int NOTIFICATION_ID_SECOND = 1002;
notify(NOTIFICATION_ID_FIRST);
notify(NOTIFICATION_ID_SECOND);
通知:
private static void notify(int id)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon).setContentTitle("My notification")
.setContentText("Test!");
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.putExtra("NOTIFICATION_ID", String.valueOf(id));
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id , mBuilder.build());
}
主要活动:
int notificationId = Integer.parseInt(getIntent().getStringExtra("NOTIFICATION_ID"));
if(notificationId == NOTIFICATION_ID_FIRST){
//handle
}else if (notificationId == NOTIFICATION_ID_SECOND){
...
}
我已阅读有关通知的 Android 开发人员指南,并且我创建了一个,如下所示:
private static void initNotificationCompatObject()
{
mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon).setContentTitle("My notification")
.setContentText("Test!");
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
}
通知本身已显示并有效。
我想做的是在按下通知后添加另一个动作。我想在我的 activity 中读取通知 ID(或在构建器的某种动作侦听器中)并根据 ID 执行某些操作。怎样才能实现这样的目标?
public static final int NOTIFICATION_ID_FIRST = 1000;
public static final int NOTIFICATION_ID_SECOND = 1002;
notify(NOTIFICATION_ID_FIRST);
notify(NOTIFICATION_ID_SECOND);
通知:
private static void notify(int id)
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon).setContentTitle("My notification")
.setContentText("Test!");
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.putExtra("NOTIFICATION_ID", String.valueOf(id));
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id , mBuilder.build());
}
主要活动:
int notificationId = Integer.parseInt(getIntent().getStringExtra("NOTIFICATION_ID"));
if(notificationId == NOTIFICATION_ID_FIRST){
//handle
}else if (notificationId == NOTIFICATION_ID_SECOND){
...
}