2016-05-20 3 views
6

Мне нужно отображать уведомление каждый день в определенное время (скажем, 7.00 утра) пользователям из моего приложения, даже когда мое приложение не было запущено . (закрытые)Запускать локальное уведомление каждый день в определенное время, даже когда приложение закрыто.

Поскольку содержание уведомления будет конкретного пользователя, я не могу использовать (GCM) нажать

Я прошел через много учебники и ответы, но я не могу иметь возможность отображать уведомления когда приложение закрыто (не работает).

Edit: Используя приведенный ниже код я могу создать уведомление в то время как мое приложение работает, если я закрою уведомление приложения обнаруживается

Вот моя основная деятельность

public class MainActivity extends AppCompatActivity { 
Button setAlarm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setAlarm = (Button) findViewById(R.id.set_alarm); 
    setAlarm.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      NotificationEventReceiver.setupAlarm(getApplicationContext()); 
     } 
    }); 
} 
@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    setIntent(intent); 
} 
} 

здесь, когда я нажимаю на кнопку установки будильника мой NotificationEventReciver позвонит

public class NotificationEventReceiver extends WakefulBroadcastReceiver { 
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE"; 
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION"; 


public static void setupAlarm(Context context) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, NotificationEventReceiver.class); 
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerAt(new Date()), 2 * 60 * 1000, alarmIntent); 

} 
private static long getTriggerAt(Date now) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 17); 
    calendar.set(Calendar.MINUTE, 10); 
    System.err.println(now.getTime()+"----->"+ calendar.getTimeInMillis()); 
    return calendar.getTimeInMillis(); 
} 

public static void cancelAlarm(Context context) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent alarmIntent = getStartPendingIntent(context); 
    alarmManager.cancel(alarmIntent); 
} 


private static PendingIntent getStartPendingIntent(Context context) { 
    Intent intent = new Intent(context, NotificationEventReceiver.class); 
    intent.setAction(ACTION_START_NOTIFICATION_SERVICE); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

public static PendingIntent getDeleteIntent(Context context) { 
    Intent intent = new Intent(context, NotificationEventReceiver.class); 
    intent.setAction(ACTION_DELETE_NOTIFICATION); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); 
    if (serviceIntent != null) { 
     System.err.println("onReceive inside not null"); 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, serviceIntent); 
    } 
} 
} 

Мой класс обслуживания, который будет запускаться при выходе тревоги.

public class NotificationIntentService extends IntentService { 

private static final int NOTIFICATION_ID = 1; 
private static final String ACTION_START = "ACTION_START"; 
private static final String ACTION_DELETE = "ACTION_DELETE"; 

public NotificationIntentService() { 
    super(NotificationIntentService.class.getSimpleName()); 
} 

public static Intent createIntentStartNotificationService(Context context) { 
    Intent intent = new Intent(context, NotificationIntentService.class); 
    intent.setAction(ACTION_START); 
    return intent; 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    System.err.println("onHandleIntent, started handling a notification event"); 
    try { 
     String action = intent.getAction(); 
     if (ACTION_START.equals(action)) { 
      System.err.println("enters the loop ACTION_START"); 
      processStartNotification(); 
     } 
    } finally { 
     WakefulBroadcastReceiver.completeWakefulIntent(intent); 
    } 
} 


private void processStartNotification() { 
    // Do something. For example, fetch fresh data from backend to create a rich notification? 
    System.err.println("inside the notify method"); 
    Intent mainIntent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setContentTitle("Scheduled Notification") 
      .setAutoCancel(true) 
      .setColor(getResources().getColor(R.color.colorAccent)) 
      .setContentText("This notification has been triggered by Notification Service") 
      .setSmallIcon(R.drawable.ic_launcher); 

    builder.setContentIntent(pendingIntent); 

    final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(NOTIFICATION_ID, builder.build()); 
} 
} 
+0

Вы можете указать данные пользователя через gcm. Имейте связь gcm reg id с пользователем или устройством пользователя и отправьте его прямо на телефон пользователя. – amalBit

+1

вам нужно использовать комбинацию в Alarm Manager, ожидающем намерения и широковещательном приемнике. – Aradhna

+0

@Aradhna, пожалуйста, пройдите код полностью! Я точно использую все три и могу создать уведомление, но! если мое приложение закрыто, мое уведомление не работает! – Praneeth

ответ

4

Попробуйте это в setUpAlarm

ме-
AlarmManager am = (AlarmManager) cont 
         .getSystemService(cont.ALARM_SERVICE); 

Intent intent = new Intent(cont, MyReceiver.class);  
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER); 

boolean isWorking = (PendingIntent.getBroadcast(cont, 1001, 

    intent, PendingIntent.FLAG_NO_CREATE) != null); 
    if (isWorking) { 
    // first cancel previous alarm 
    Intent intent = new Intent(cont, MyReceiver.class);// the same as up 
    intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);// the same as up 
    PendingIntent pI = PendingIntent.getBroadcast(cont, 1001, 
    intent, PendingIntent.FLAG_CANCEL_CURRENT);// the same as up 
    am.cancel(pI);// important 
    pI.cancel();// important 
    } else { 
    Intent intent1 = new Intent(cont, MyReceiver.class); 
    intent1.setAction(MyReceiver.ACTION_ALARM_RECEIVER); 

    PendingIntent pendingIntent = PendingIntent 
    .getBroadcast(cont, 0, intent1, 
    PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager am = (AlarmManager) cont 
    .getSystemService(cont.ALARM_SERVICE); 

    am.setRepeating(AlarmManager.RTC_WAKEUP, 
    calendar.getTimeInMillis(), 
    AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 

Надежда это помогает! :)

+0

Я изменил время повторения до 2 минут для тестирования! – Praneeth

+0

Он всегда идет внутри isWorking true loop! он не собирается еще часть – Praneeth

+0

Да, это будет, и это то, что вы должны проверить. Часть else будет запускаться только при первом запуске приложения. – Aradhna

Смежные вопросы