Начать сервис извещения
можно запустить сервис из уведомления.
Обычный способ запуска активности работает отлично, но мне нужно предварительно проверить данные перед запуском приложения.
Я тестировал его с включением действительной службы в намерении уведомления, но ничего не происходит.
Ответы
Ответ 1
Можно запустить сервис из уведомления.
Вы должны использовать PendingIntent.getService вместо 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);
Ответ 2
Создайте широковещательный приемник, получите сообщение от уведомления и затем запустите службу.
Ответ 3
private void createNotification(String message)
{
Intent intent = new Intent(this, Yourservice.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getService(this, 0 /* Request code */, intent,
0);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icond)
.setContentTitle("Start Launcher")
.setContentText(message)
.setAutoCancel(true)
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_NOTIFICATION , notificationBuilder.build());
}