2017-01-09 62 views
-4

工作我想在一定的時間來推送通知。爲了執行此操作,我正在觸發最終調用顯示通知的廣播接收器的警報。通知沒有在安卓

private void setAlarmToCallNotificationService(Context context, int request_code, String notificationText, String notificationTitle) { 

     Log.i("Inside notification,","Yes"); 

     Intent intent = new Intent(context, notificationService.class); 
     intent.putExtra("Notification_title",notificationTitle); 
     intent.putExtra("Notification_text",notificationText); 


     //hit the notification At the 8.00 in the morning 
     Calendar notificationCalendar=Calendar.getInstance(); 
     notificationCalendar.set(Calendar.HOUR_OF_DAY,16); 
     notificationCalendar.set(Calendar.MINUTE,29); 
     notificationCalendar.set(Calendar.SECOND,0); 

     Long time=notificationCalendar.getTimeInMillis(); 

     System.out.println("NOTIFICATION Time is "+notificationCalendar.get(Calendar.HOUR_OF_DAY)+" "+notificationCalendar.get(Calendar.MINUTE)); 
     Log.i("Target",time.toString()); 

     //final int _id = (int) System.currentTimeMillis(); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT); 

     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent); 

    } 

我檢查了報警正在啓動。

public class notificationService extends BroadcastReceiver { 

    public static String TAG="notificationService"; 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     String notificatioTitle=intent.getExtras().getString("Notification_title"); 
     String notificationMsg=intent.getExtras().getString("Notification_text"); 

     Log.i(TAG,"Notification title "+notificatioTitle); 
     Log.i(TAG,"Notification msg "+notificationMsg); 

     NotificationCompat.Builder notification=new NotificationCompat.Builder(context) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(notificatioTitle) 
       .setContentText(notificationMsg); 

     NotificationManager mNotificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(0,notification.build()); 
    } 


} 

清單文件:

<receiver android:name=".notificationService" /> 
+0

爲什麼這麼多downvotes?誰能解釋一下。 –

回答

0

難道你的接收器接收在預期的時間意圖是什麼?

我的意思是......下面做任何打印的代碼呢?

Log.i(TAG,"Notification title "+notificatioTitle); 
Log.i(TAG,"Notification msg "+notificationMsg); 

如果沒有,請檢查您是否在您設備DateTimeSettings, 您可能需要使用NTP時間設置報警使用Settings.System.AUTO_TIME

long currentTime = System.currentTimeMillis() - ntpTimeOffset; 
long timeToWaitForTrigger = calendar.getTimeInMillis() - currentTime; 
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeToWaitForTrigger, pendingIntent); 
+0

我使用幾乎相同的功能,用於啓動對話框,然後但這是工作,但爲什麼不這是工作。@斯基特 –

+0

燁我通知接收機沒在預期的時間的意圖。但是,我用相同的代碼發射警報,但是時間不同,這些代碼會呼叫另一個廣播接收器(此廣播接收器正在工作)進行一些計算,然後將此通知服務稱爲此通知服務(此廣播接收器未接收到此意圖)接收器發射通知。 –