原 android本地推送

2021-09-09 01:26:15 字數 2541 閱讀 2070

android本地推送的實現原理:開啟乙個broadcastreceiver和乙個alarmmanager,鬧鐘設定推送喚醒時間,broadcastreceiver一直在檢測是否應該推送。

目前遺留問題,好多手機 關閉應用 service被殺死,無法接受推送。各種重啟service我也試了 小公尺手機就是不好使! 要是確保service不死  完美收到推送

public static string pushaction = "cn.***.pushaction";

pushdata="1|2|09:50|內容^2|2|09:58|內容"  // id|型別|時間|內容

設定重複型鬧鐘

sharedpreferences sharedpreferences = cocos2dxactivity.getcontext().getsharedpreferences("sp", cocos2dxactivity.mode_private);

sharedpreferences.editor editor = sharedpreferences.edit();

editor.putstring("key", pushdata);

editor.commit();

intent intent =new intent(cocos2dxactivity.getcontext(), pushreceiver.class);

intent.setaction(pushaction);

pendingintent sender=pendingintent.getbroadcast(cocos2dxactivity.getcontext(), 100, intent, pendingintent.flag_cancel_current);

alarmmanager alarm=(alarmmanager)cocos2dxactivity.getcontext().getsystemservice(context.alarm_service);

alarm.setrepeating(alarmmanager.rtc,system.currenttimemillis(),60*1000, sender); --設定每隔一分鐘傳送一次pushaction 設定重複執行

設定一次型鬧鐘

long t = long.parselong(time)*1000+system.currenttimemillis();

intent intent =new intent(cocos2dxactivity.getcontext(), pushreceiver.class);

intent.setaction(pushaction);

intent.putextra("id", id);--注意這個id最好唯一,假如設定多條推送時 ,id必須唯一 要不就亂了

intent.putextra("content", body);

intent.putextra("type",2); //對應pushreceiver 型別判斷

pendingintent sender=pendingintent.getbroadcast(cocos2dxactivity.getcontext(), id, intent, pendingintent.flag_cancel_current); --注意第二個引數 一定唯一 當有多條推送的時候

alarmmanager alarm=(alarmmanager)cocos2dxactivity.getcontext().getsystemservice(context.alarm_service);

alarm.set(alarmmanager.rtc, t, sender);--從當前開始 間隔time之後 觸發推送

觸發推送的實現 pushreceiver類

@override

public void onreceive(context arg0, intent intent) }}

public static void pushnotify(context ctx)

else

int mimute = calendar.getinstance().get(calendar.minute);

string strmimute= "";

if (mimute<=9)

else

for(int i=0;inoti.flags = notification.flag_auto_cancel;

intent intent = new intent(ctx, pokemon.class);

intent.setflags(intent.flag_activity_clear_top| intent.flag_activity_new_task);

pendingintent contentintent = pendingintent.getactivity(ctx, id,intent, pendingintent.flag_update_current);

noti.setlatesteventinfo(ctx,title, body, contentintent);

nm.notify(id, noti);

}androidmanifest.xml配置

android 之推送(本地推送遠端推送)

推送分為遠端推送和本地推送,都可以通過推送訊息跳轉到一些應用或者活動介面,本地推送幾行 就可以實現,遠端推送就需要借助一些平台的力量 1 本地推送 notificationmanager nm notificationmanager getsystemservice context.notifica...

iOS推送 實現本地推送

第一步 建立本地推送 建立乙個本地推送 uilocalnotification notification uilocalnotification alloc init autorelease 設定 10秒之後 nsdate pushdate nsdate datewithtimeintervalsi...

iOS推送 實現本地推送

第一步 建立本地推送 建立乙個本地推送 uilocalnotification notification uilocalnotification alloc init autorelease 設定10秒之後 nsdate pushdate nsdate datewithtimeintervalsin...