2012-11-18 210 views
0

我需要一個關於android系統推送通知的教程。 我不需要GCM的實現,我只需要推送由應用程序的警報管理器調用的通知。Android上的推送通知 - 不是GCM

這可能嗎?

+0

我懷疑您希望在任何平臺上將其描述爲「推送通知」。 – CommonsWare

+0

是的,我想要的東西只是「通知」。 – svennergr

回答

3

另請參閱scheduling-repeatable-android-notification-in-different

對於使用AlaramManager,我們首先需要聲明的廣播接收器清單文件作爲,

接收器的android: 「TimeAlarm」 NAME = />

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); 
    } 
    } 
+0

通知部分幫助了我。非常感謝! – svennergr