0

我有一个应用程序来设置状态栏通知。我可以设置它们,当我单击状态栏通知时,它会打开应用程序。问题是如果我按下主页按钮,它应该将应用程序发送到后台,当通知到来时,应用程序会自动出现在前面。

 public void setNotification(String date, String alertText, String alertTitle, String sound,String notificationId,int repeatOption, long eventsJni) {
    alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent("sprat.notifier.run");

    intent.putExtra("alertText", alertText);
    intent.putExtra("alertTitle", alertTitle);
    intent.putExtra("sound", sound);
    int id = (int)Integer.valueOf(notificationId);
    intent.putExtra("notificationId", id);


    PendingIntent pendingIntent;
    long time = getDate(date).getTime();
    if(repeatOption == 1){
        pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), id, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        alarm.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    }

}

当闹钟时间到来时,它会在另一个活动中设置通知。

public class Notifications extends Activity {
        private int date;
        private String alertText;
        private String alertTitle;
        private String sound;
        private int id;
        private int repeatOption;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            Intent intent = getIntent();
            if (intent == null){
                finish();
                return;
            }
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
            if(intent.getExtras().getString("notif_id") != null && intent.getExtras().getString("notif_type") != null) {
                mNotificationManager.cancel((int)Integer.valueOf(intent.getExtras().getString("notif_id")));
                Intent i = new Intent(this.getApplicationContext(), A.class);
                i.putExtra("notif_id", intent.getExtras().getString("notif_id"));
                i.putExtra("notif_type", intent.getExtras().getString("notif_type"));
                startActivity(i);
                finish();
                return;
            }

            set(intent);            

        }

        private void set(Intent intent){

            alertText = intent.getExtras().getString("alertText");
            alertTitle = intent.getExtras().getString("alertTitle");
            sound = intent.getExtras().getString("sound");
            id = intent.getExtras().getInt("notificationId");

            Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(100);


            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
            int icon = R.drawable.icon;
            CharSequence tickerText = alertTitle;
            long when = System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText, when);

            Context context = getApplicationContext();
            CharSequence contentTitle = alertTitle;
            CharSequence contentText = alertText;
            Intent notificationIntent = new Intent(this, SpNotifications.class);
            notificationIntent.putExtra("notif_id", String.valueOf(id));
            notificationIntent.putExtra("notif_type", "Local");


            PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            notification.defaults  |= Notification.DEFAULT_VIBRATE;
            notification.defaults  |= Notification.DEFAULT_SOUND;
            mNotificationManager.notify(id, notification);

            finish();
        }
4

1 回答 1

0

我不确定这里发生了什么。如果您单击通知,应用程序应该会出现在前台。单击主页时,应出现主屏幕。该应用程序无法自动出现在最前面以获取通知。

检查文档中的新通知指南。特别是,它描述了如何处理从通知启动的应用程序的导航。

于 2012-11-06T19:48:10.247 回答