在Android上推送通知 - 而不是GCM

时间:2012-11-18 22:30:33

标签: android push-notification

我需要一个关于Android系统推送通知的教程。 我不需要实施GCM,我只需要应用程序的警报管理员调用推送通知。

这可能吗?

1 个答案:

答案 0 :(得分:3)

另请参阅scheduling-repeatable-android-notification-in-different

对于使用AlaramManager,首先我们需要将Manifest文件中的BroadcastReceiver声明为,

接收器android:name =“。TimeAlarm”/>

public class CustomAlarm extends Activity {

     AlarmManager am;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      setOneTimeAlarm();
     }

     public void setOneTimeAlarm() {
      Intent intent = new Intent(this, TimeAlarm.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);
      am.set(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + (5 * 1000), pendingIntent);
     }

     public void setRepeatingAlarm() {
      Intent intent = new Intent(this, TimeAlarm.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_CANCEL_CURRENT);
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        (5 * 1000), pendingIntent);
     }

    }


    public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Nithin";
      CharSequence message = "Crazy About Android...";
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(), 0);
      Notification notif = new Notification(R.drawable.icon,
        "Crazy About Android...", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);
     }
    }