通知在android中不起作用
Posted
技术标签:
【中文标题】通知在android中不起作用【英文标题】:notification is not working in android 【发布时间】:2017-01-09 12:22:26 【问题描述】:我正在尝试在特定时间推送通知。为此,我触发了警报,该警报最终调用了显示通知的广播接收器。
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" />
【问题讨论】:
为什么有这么多反对票?谁能解释一下。 【参考方案1】:您的接收者是否在预期时间收到意图?
我的意思是...下面的代码是否打印了任何内容?
Log.i(TAG,"Notification title "+notificatioTitle);
Log.i(TAG,"Notification msg "+notificationMsg);
如果没有,请检查您是否在设备DateTimeSettings
中使用Settings.System.AUTO_TIME
,
您可能需要使用 NTP 时间来设置闹钟。
long currentTime = System.currentTimeMillis() - ntpTimeOffset;
long timeToWaitForTrigger = calendar.getTimeInMillis() - currentTime;
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeToWaitForTrigger, pendingIntent);
【讨论】:
我使用几乎相同的功能来启动对话框,但这是有效的,但为什么不有效。@Skeeter 是的,我的通知接收器没有在预期的时间收到意图。但是我用相同的代码发出警报,但时间不同,它调用另一个广播接收器(这个广播接收器正在工作)进行一些计算,然后调用这个通知服务(这个广播接收器没有接收到意图)接收器来触发向上通知。以上是关于通知在android中不起作用的主要内容,如果未能解决你的问题,请参考以下文章