简体   繁体   English

通知在android中不起作用

[英]notification is not working in android

I am trying to push notification at the certain time.我正在尝试在特定时间推送通知。 To perform this I am triggering the alarm which eventually call the broadcast receiver that show the notifications.为了执行此操作,我触发了最终调用显示通知的广播接收器的警报。

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);

    }

I checked that the alarm is firing up.我检查了警报是否响起。

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());
    }


}

Manifest file:清单文件:

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

Did your receiver receive intent at expected time?您的接收者是否在预期时间收到了意图?

I mean... did the code below print anything at all?我的意思是……下面的代码有没有打印任何东西?

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

If no, check if you use Settings.System.AUTO_TIME in you device DateTimeSettings , You may need to use NTP time to set alarm.如果没有,请检查您是否在您的设备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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM