通知没有在android中的特定时间触发

时间:2014-07-10 06:09:12

标签: android notifications

嗨在我的申请中,我会在特定时间发布通知。

例如: 今天我想说生日祝福特定的时间我想发送特定时间的通知。因为我设定时间,如果那个时间与当前时间匹配意味着我想要点那样的通知生日快乐。

现在,我的问题是,如果我在两次设置不同的时间。第一次覆盖第二次,它会触发发送时间,我设置的时间是触发通知的时间。但我想要同时触发时间。 / p>

任何人都可以帮我解决这个问题。

MyView类

public class MyView extends Activity {
    TextView tv;
     Button btn;
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Calendar Calendar_Object = Calendar.getInstance();
         Calendar firingCal = Calendar.getInstance();


           firingCal.set(Calendar.HOUR_OF_DAY, 11);
            firingCal.set(Calendar.MINUTE, 27);
            firingCal.set(Calendar.SECOND, 0);


            Calendar firingCal1 = Calendar.getInstance();


               firingCal1.set(Calendar.HOUR_OF_DAY, 11);
                firingCal1.set(Calendar.MINUTE, 28);
                firingCal1.set(Calendar.SECOND, 0);



        // MyView is my current Activity, and AlarmReceiver is the
        // BoradCastReceiver
        Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
                0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        /*
         * The following sets the Alarm in the specific time by getting the long
         * value of the alarm date time which is in calendar object by calling
         * the getTimeInMillis(). Since Alarm supports only long value , we're
         * using this method.
         */

        alarmManager.set(AlarmManager.RTC_WAKEUP, firingCal.getTimeInMillis(),
                pendingIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP, firingCal1.getTimeInMillis(),
                pendingIntent);
    }
}

NotificationService类

public class NotificationService extends Service {

    private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        // Getting Notification Service
        mManager = (NotificationManager) this.getApplicationContext()
                .getSystemService(
                        this.getApplicationContext().NOTIFICATION_SERVICE);
        /*
         * When the user taps the notification we have to show the Home Screen
         * of our App, this job can be done with the help of the following
         * Intent.
         */
        Intent intent1 = new Intent(this.getApplicationContext(), MyView.class);

        Notification notification = new Notification(R.drawable.ic_launcher,
                "Welcome", System.currentTimeMillis());

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                this.getApplicationContext(), 0, intent1,
                PendingIntent.FLAG_UPDATE_CURRENT);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.setLatestEventInfo(this.getApplicationContext(),
                "Wishes", "Happy Birthday",
                pendingNotificationIntent);

        mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

提前致谢

2 个答案:

答案 0 :(得分:0)

试试这个:

 @Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    // Getting Notification Service
    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService(
                    this.getApplicationContext().NOTIFICATION_SERVICE);

    long when = System.currentTimeMillis(); // ADD THIS LINE
    /*
     * When the user taps the notification we have to show the Home Screen
     * of our App, this job can be done with the help of the following
     * Intent.
     */
    Intent intent1 = new Intent(this.getApplicationContext(), MyView.class);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "Welcome", System.currentTimeMillis());

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(this.getApplicationContext(),
            "Wishes", "Happy Birthday",
            pendingNotificationIntent);

    mManager.notify((int)when, notification); // Instead of '0' add 'when'. You will get notifications based on time.
}

您正在使用:mManager.notify(0, notification);

这只会一次显示一个通知并覆盖其他通知。

您应该使用:mManager.notify(uniqueid, notification);

这将显示基于uniqueid的所有通知。

希望这有帮助。

答案 1 :(得分:0)

试试这段代码:

Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, (int) whenToFire);

        Intent intent = new Intent(context, LocalNotificationReceiver.class);
        intent.putExtra("title", title);
        intent.putExtra("message", message);
        intent.putExtra("class", CIIConstants.LAUNCHER_ACTIVITY_PATH);
        intent.putExtra("index", notificationIndex);

        PendingIntent sender = PendingIntent.getBroadcast(context, notificationIndex, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

在接收时使用此代码:

NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, LauncherActivity.class), 0);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message);
            mBuilder.setContentIntent(contentIntent);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);

            mgr.notify(notificationIndex, mBuilder.build());

其中 notificationIndex 应该是唯一的。对于每个通知

如果你想取消任何通知,你可以通过传递像这样的唯一身份证来做到这一点

public static void cancelNotification(Context context, int notificationIndex)
{
    Intent intent = new Intent(context, LocalNotificationReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, notificationIndex, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(sender);
}