2017-01-09 78 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

烨我通知接收机没在预期的时间的意图。但是,我用相同的代码发射警报,但是时间不同,这些代码会呼叫另一个广播接收器(此广播接收器正在工作)进行一些计算,然后将此通知服务称为此通知服务(此广播接收器未接收到此意图)接收器发射通知。 –