2016-10-11 4 views
1

특정 시간에 사용자에게 표시되는 일부 인앱 알림이 있지만 응용 프로그램을 닫을 때 아무 것도 표시되지 않습니다.응용 프로그램이 닫힐 때 AlarmManager가 실행되지 않습니다.

설정 경보 :

Intent alarmIntent = new Intent(mMotherActivity, ReminderAlarmManager.class); 

if (ReminderNotificationType.CHANGE_LENS.equals(notificationType)) { 
    alarmIntent.putExtra("NOTIFICATION_TYPE", "REMINDER"); 
} else { 
    alarmIntent.putExtra("NOTIFICATION_TYPE", "ORDER"); 
} 

long scTime = alarmDate.getTime(); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(mMotherActivity, 0, alarmIntent, 0); 
AlarmManager alarmManager = (AlarmManager) mMotherActivity.getSystemService(mMotherActivity.ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, scTime, pendingIntent); 

방송 수신기 : 그것은 scTime에 관해서

public class ReminderAlarmManager extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     String notificationType = intent.getStringExtra("NOTIFICATION_TYPE"); 
     if(notificationType.equalsIgnoreCase("ORDER")) 
     { 
      Intent i = new Intent(context, OrderReminderNotificationService.class); 
      context.startService(i); 
     } 
     else if(notificationType.equalsIgnoreCase("REMINDER")) 
     { 
      Intent i = new Intent(context, ReminderNotificationService.class); 
      context.startService(i); 
     } 
    } 
} 

그래서, 앱이 종료되는 경우에도, 나는 알림을 트리거하고 싶습니다.

public class OrderReminderNotificationService extends IntentService { 

    public OrderReminderNotificationService(String name) { 
     super(name); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     Context context = getApplicationContext(); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
      context); 

     Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     mBuilder.setContentTitle("Notification"); 
     mBuilder.setContentText(context.getString(R.string.renewOrderNotificationMsg)); 
     mBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     mBuilder.setSound(uri); 
     mBuilder.setAutoCancel(true); 

     Intent notificationIntent = new Intent(this, LoginActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(2, mBuilder.build()); 
} 

그와 관련된 매니페스트 부분 :

<receiver android:name="com.company.utils.ReminderAlarmManager"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

을하지만 아무것도가 표시되지 않습니다 내가 잘못 뭐하는 거지 ... 다음과 같이 그래서 나는 PendingIntent하여 서비스를 호출하는거야?

+0

당신은'BroadcastReceiver'에서 전화 서비스를하고 있습니까? – Marat

+0

전체 코드로 내 게시물을 편집했습니다. BroadCastreceiver를 게시하고 AlarmManager의 설정을 완료했습니다. 나는 BOOT_COMPLETED 같은 것을 매니 페스트에서해야합니까? –

+0

매니페스트 파일도 필요합니다. 매니페스트에'broadcastReceiver'와'service'를 등록하셨습니까? – Marat

답변

2

귀하의 AndroidManifest.xml 다음과 같이해야한다 :

<receiver android:name=".ReminderAlarmManager"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

<service android:name=".OrderReminderNotificationService "/> 
+1

질문이 하나 더 있습니다. Receiver를 BOOT_COMPLETED로 실행하라는 지시를 받으면 devide가 부팅 될 때 Receiver가 트리거 될 것임을 알았습니다. 하지만 앱이 종료 된 경우에도 알람 관리자에게 전달 된 특정 시간에 알람을 시작하고 싶습니다. 그것은 그렇게 올바르게 작동해야합니까? –

+0

전화가 꺼지면 모든 알람이 삭제됩니다. 따라서 우리는 시스템이 intenr-filter에있는 모든 수신자에게 보내는'BOOT_COMPLETED' 액션을 가지고 있습니다. 이것이 시스템이 재부팅 된 것을 의미하며 경보를 재설정해야합니다. 그러나 재부팅 후 경보 삭제에 신경 쓰지 않으면'BOOT_COMPLETED'와 함께 의도 필터가 필요 없습니다. 앱이 종료 된 경우에도 알람이 시작됩니다. – Marat

0

알람 관리자가 특정 휴대폰에 텐트 서비스를 트리거하지 않습니다. Oneplus 3T에서 하루 종일 앱을 테스트하고 있었는데 코드에 아무 문제가 없다는 것을 깨달았습니다. Moto G에서 동일한 앱을 테스트 한 결과 예상대로 작동합니다.

관련 문제