5

私のアプリケーションが起動すると、APIコールが実行され、結果に基づいて通知がスケジュールされます。これは約10の通知が予定されていることになります。実際の通知に表示されるタイムスタンプが正しくないという問題があるようです。今後の通知でタイムスタンプが正しくない

これらの通知を作成してからAlarmManagerでアラームをスケジュールするので、通知に表示されるデフォルトの時刻は、通知が作成された時刻(System.currentTimeMillis())になります。

私はNotification.Builder.setWhen()メソッドを使用して、前述のアラームのスケジュールに使用している時間に設定しようとしました。ただし、通知が指定された正確な時刻に配信されることが保証されていないため、これは少し良くなりました。過去に数分の通知を受け取ることがよくあります。

また、私は.notify()が実際に呼び出される直前に、手動で私のBroadcastReceiverに通知のwhenフィールドを上書きしようとした:

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification_id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

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

     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     notification.when = System.currentTimeMillis(); 
     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
     notificationManager.notify(id, notification); 

    } 
} 

しかし、上記のシナリオでは、.whenが無視されるようです。

率直に言えば、通知に表示されるタイムスタンプを実際に表示される時刻にする方法を探しています。

答えて

2

あなたの通知の情報を補足として渡してから、BroadcastReceiverの内部に通知を作成することをお勧めします。これにより、発行される直前に通知が作成されるため、AlarmManagerがBroadcastReceiverをトリガーするのと同じ時間になります。

あなたが通知をスケジュールしているどこから:

private void scheduleNotification(){ 

    // Create an intent to the broadcast receiver you will send the notification from 
    Intent notificationIntent = new Intent(this, SendNotification.class); 

    // Pass your extra information in 
    notificationIntent.putExtra("notification_extra", "any extra information to pass in"); 

    int requestCode = 1; 

    // Create a pending intent to handle the broadcast intent 
    PendingIntent alarmIntent = PendingIntent 
       .getBroadcast(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Set your notification's trigger time 
    Calendar alarmStart = Calendar.getInstance(); 
    alarmStart.setTimeInMillis(System.currentTimeMillis()); 
    alarmStart.set(Calendar.HOUR_OF_DAY, 6); // This example is set to approximately 6am 

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    // Set the alarm with the pending intent 
    // be sure to use set, setExact, setRepeating, & setInexactRepeating 
    // as well as RTC_WAKEUP, ELAPSED_REALTIME_WAKEUP, etc. 
    // where appropriate 
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStart.getTimeInMillis(), alarmIntent); 
} 

を次に、あなたのBroadcastReceiverのonReceive内側:

String notificationExtra = null; 

// Retrieve your extra data 
if(intent.hasExtra("notification_extra")){ 
    notificationExtra = intent.getStringExtra("notification_extra"); 
} 

//Build the notification 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 
mBuilder.setSmallIcon(notificationIcon) 
     .setContentTitle(notificationTitle) 
     .setContentText(notificationMessage) 
     .setAutoCancel(true); // Use AutoCancel true to dismiss the notification when selected 

// Check if notificationExtra has a value 
if(notificationExtra != null){ 
    // Use the value to build onto the notification 
} 

//Define the notification's action 
Intent resultIntent = new Intent(context, MainActivity.class); // This example opens MainActivity when clicked 

int requestCode = 0; 

PendingIntent resultPendingIntent = 
     PendingIntent.getActivity(
       context, 
       requestCode, 
       resultIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT 
     ); 

//Set notification's click behavior 
mBuilder.setContentIntent(resultPendingIntent); 

// Sets an ID for the notification 
int mNotificationId = 1; 

// Gets an instance of the NotificationManager service 
NotificationManager mNotifyMgr = 
     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

// Builds the notification and issues it. 
mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
+0

感謝を助ける

public class NotificationPublisher extends BroadcastReceiver { public static String NOTIFICATION_ID = "notification_id"; public static String NOTIFICATION = "notification"; public void onReceive(Context context, Intent intent) { int id = intent.getIntExtra(NOTIFICATION_ID, 0); Intent intent = new Intent(context, YourTargetActivity.class); intent.putExtra("KEY_ID", id); // Pass extra values if needed PendingIntent pI = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Notification Notification notification = new Notification.Builder(context) .setContentTitle("This is notification title") .setContentText("This is notification text") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pI).build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; // Notification Manager NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager .notify(id, notification); } } 

願っています! – xbadal

+0

ありがとうサミー!完全に働いた – xbadal

+0

あなたは複数の通知を作成する方法を教えてくれますか?それはmNotificationIdを提供することによって行うことができますか?私は同じをキャンセルする必要があります! – xbadal

0

あなたNotificationPublisherのonReceive()メソッドは、指定されたtimeとしてalarmトリガをスケジュールした場合にのみinvokedになります。 onReceive()メソッドから通知を作成すると、現在の時刻が確実に表示されます。 .whenまたは.setWhen()メソッドを使用する必要はありません。

は、この方法を試してください。

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification_id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 

     // Notification 
     Notification notification = new Notification.Builder(context)  
      .setContentTitle("This is notification title") 
      .setContentText("This is notification text") 
      .setSmallIcon(R.mipmap.ic_launcher).build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Notification Manager 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager .notify(id, notification); 
    } 
} 

あなたは、あなたがPendingIntentを使用し、Notificationに設定することができ、Notificationをクリックしたときactivityにリダイレクトします。これは私はこの1つを試してみましょう@sammy〜

関連する問題