从开始通知服务通知

2023-09-13 00:24:41 作者:娘们的外表,爷们的心

,才有可能从通知启动服务。 开始活动的正常方式工作的很好,但我在实际开始应用程序需要的数据约pre检查。

我已经与包括在通知的意图有效的服务进行了测试,但没有任何反应。

解决方案   

有可能从通知启动服务。

您必须使用PendingIntent.getService而不是pendingIntent.getActivity。

 意图notificationIntent =新的意图(mContext,HandleNotificationClickService.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext,0,notificationIntent,0);

通知通知=新的通知(图标,tickerText,System.currentTimeMillis的());
notification.setLatestEventInfo(mContext,contentTitle,contentText,pendingIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

NotificationManager notificationManager =(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(CALLER_ID_NOTIFICATION_ID,通知);
 
鹤山市行政服务中心从2月4日开始暂停对外服务的通告

is it possible to start a service from a notification. The normal way of starting an activity is working perfectly, but I need some pre checks of data before actually starting the app.

I've tested it with including a valid service in the notification intent, but nothing happens.

解决方案

It is possible to start a service from a notification.

You have to use PendingIntent.getService instead of pendingIntent.getActivity.

Intent notificationIntent = new Intent(mContext, HandleNotificationClickService.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0);

Notification notification = new Notification(icon, tickerText,System.currentTimeMillis());
notification.setLatestEventInfo(mContext,contentTitle , contentText, pendingIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(CALLER_ID_NOTIFICATION_ID, notification);