0

這是我的代碼。它工作正常,如果我在一個單獨的Android工作室項目中使用它。但是當我將代碼集成到另一個項目時,它會造成問題。 如果應用程序已打開,我可以收到通知。但在應用關閉時,通知未收到。在BroadcastReceiver中創建的通知在應用程序關閉時不起作用

另外我觀察到一件事 - 我將鬧鐘設置爲1:10(假設)並關閉,然後重新打開1.09的應用程序。我在1.10 !!收到通知!!

我無法確定發生了什麼。請告訴我,即使我在做愚蠢的錯誤,謝謝。

public class BroadCast { 

public void broadcastIntent(int selected, Context context, long userMilli) { 
    Intent intent = new Intent(context.getApplicationContext(), ReminderReceiver.class); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("selected", selected); 
    intent.putExtras(bundle); 
    intent.setAction("android.media.action.DISPLAY_NOTIFICATION"); 
    intent.addCategory("android.intent.category.DEFAULT"); 

    Calendar calendar = Calendar.getInstance(); 
    long curMilli = calendar.getTimeInMillis(); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), selected, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) 
     alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (userMilli-curMilli), pendingIntent); 
    else 
     alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (userMilli-curMilli), pendingIntent); 
} 
} 

上面的代碼創建一個intentbroadcast使用alarmManager

public class ReminderReceiver extends BroadcastReceiver { 

public ReminderReceiver() { 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 
    int selected = bundle.getInt("selected"); 
    sendNotification(context, selected); 
} 

private void sendNotification(Context context, int selected) { 
    String notificationContentText = "String to display"; 

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 

    builder.setSmallIcon(R.drawable.alarm) 
      .setContentTitle("Title") 
      .setPriority(Notification.PRIORITY_MAX) 
      .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
      .setContentText(notificationContentText) 
      .setColor(Color.rgb(58,95,205)); 

    notificationManager.notify(selected, builder.build()); 
} 
} 

以上代碼構建通知

這是我AndroidManifest文件

<receiver 
     android:name=".ReminderReceiver" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="android.media.action.DISPLAY_NOTIFICATION"/> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
+0

檢查我的答案在這裏,當它的應用程序關閉,當設備啓動時打開並「重新打開」的作品http://stackoverflow.com/a/38474519/3626214 – Aspicas

+0

嘿,我想你的方式,仍然不工作。你能告訴我你在哪裏調用該代碼中的MyService。 – hegdeGanesh

+0

你可以使用'startService'作爲'Intent serviceIntent = new Intent(this,MyService.class); startService(serviceIntent);' – Aspicas

回答

0

這不是廣播接收器或服務的問題。我不得不在我的手機中更改我的通知設置。我使用華碩,你必須檢查電源管理選項。如果它處於省電模式,則通知在應用程序關閉時被拒絕。

相關問題