从 AppWidgetProvider 或服务 class 中的应用小部件创建新的 Android 通知?
Create new Android Notification from app widget in AppWidgetProvider or Service class?
问)是否可以从应用程序小部件创建系统通知(显示在下拉阴影中)?
特别是我想从我的:
RemoteFetchService.class
,获取我的远程数据...
或者
AppWidgetProvider.class
,更新小部件 UI。
是的,可以从服务创建通知。
这是一个示例代码。
private void triggerNotification() {
CharSequence title = "title";
CharSequence message = "message";
NotificationManager notificationManager;
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
notification = new Notification(
R.drawable.ico_event, "Notifiy.. ",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), title, message, pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
}
请记住,这是我从我的应用程序中获取的代码,因此您可能需要替换一些值以匹配您的应用程序。
问)是否可以从应用程序小部件创建系统通知(显示在下拉阴影中)?
特别是我想从我的:
RemoteFetchService.class
,获取我的远程数据...
或者
AppWidgetProvider.class
,更新小部件 UI。
是的,可以从服务创建通知。 这是一个示例代码。
private void triggerNotification() {
CharSequence title = "title";
CharSequence message = "message";
NotificationManager notificationManager;
notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
notification = new Notification(
R.drawable.ico_event, "Notifiy.. ",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), title, message, pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
}
请记住,这是我从我的应用程序中获取的代码,因此您可能需要替换一些值以匹配您的应用程序。