推送通知重复每秒

时间:2016-05-10 09:08:09

标签: android push-notification android-service android-broadcastreceiver

大家好我正在尝试使用闹钟管理器创建推送通知,其中推送通知必须在早上8点出现一切都很好但通知也即将到来但是在上午8点通知完成后,通知即将开启应用程序或清除从最近或每一小时通知即将到来请帮我解决过去1周的这个问题

MainActivity.class

am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent notificationintent = new Intent(this, Notify.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, notificationintent, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationintent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
            Calendar calnotify = Calendar.getInstance();
            Calendar now=Calendar.getInstance();
            //Time Alaram manager
            calnotify.set(Calendar.HOUR_OF_DAY, 10);
            calnotify.set(Calendar.MINUTE, 00);
            calnotify.set(Calendar.SECOND, 0);
            if (now.after(calnotify)){
                calnotify.add(Calendar.DATE,1);
            }
            am.setRepeating(AlarmManager.RTC_WAKEUP, calnotify.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

Notify.class

// BroadCastReceiver类

public class Notify extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent pushservice=new Intent(context,PushNotification.class);
        pushservice.setData((Uri.parse("custom://"+System.currentTimeMillis())));
        context.startService(pushservice);
    }

}

PushNotification.class

public class PushNotification extends Service {


  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }

    @Override
    public void onCreate() {
        super.onCreate();
        Resources resources = getResources();
        getnotify(resources);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Resources resources = getResources();
        getnotify(resources);
        /*Notify notify=new Notify();
        notify.abortBroadcast();*/
    }

    private void getnotify(Resources resources) {
        Bitmap icon = BitmapFactory.decodeResource(resources,
                R.drawable.sr_notification_icon);
        PugNotification.with(this)
                .load()
                .title(getResources().getString(R.string.app_name))
                .message("Latest Chitka")
                .bigTextStyle(getResources().getString(R.string.app_name))
                .smallIcon(getNotificationIcon())
                .largeIcon(icon)
                .flags(android.app.Notification.DEFAULT_ALL).autoCancel(true).click(MyCalendarActivity.class)
                .simple()
                .build();

    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.sr_icon : R.drawable.sr_notification_icon;
    }
}

1 个答案:

答案 0 :(得分:0)

从应用程序的主Activity调用时,看起来设置警报的代码多次执行。一旦设置了警报,您应该设置一个设置为true的标志。下次根据此标志跳过警报设置。