最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 接收推送消息跳轉到指定頁面的方法

 更新時間:2018年01月11日 09:25:59   作者:xxq2dream  
這篇文章主要介紹了Android 接收推送消息跳轉到指定頁面的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

問題的提出

本次接入的是個推,其他家的推送沒有研究過,思路應該是類似的

App在前臺,這個時候需要彈出一個對話框,提醒用戶有新的消息,是否要查看,查看的話跳轉到指定頁面

App在后臺,或是App進程已經被系統(tǒng)回收,收到推送后App進程會被個推拉起。這時候要展示通知,點擊通知欄打開App并跳轉到目標頁面,關閉目標頁面后需要返回到應用首頁,而不是直接推出App

實現(xiàn)思路

App在前臺時,彈出Dialog提醒用戶有新消息,但是最新版的個推文檔接收推送消息是繼承IntentService,無法獲取彈出Dialog所需要的Context(注意不能用getApplicationContext()),所以采用Dialog樣式的Activity來實現(xiàn)

App在后臺時,如果直接在PendingIntent中傳目標Activity的Intent,則在退出目標Activity時會直接退出應用,感覺像是閃退了一樣;如果是跳轉到首頁,然后在首頁中檢測是否是由點擊通知進入應用的來進行跳轉,這樣的話首頁就會閃屏。綜上方法都不是很理想,一個比較好的解決方案是給PendingIntent傳遞一個Intent數(shù)組,分別放置目標Activity和首頁,這樣效果比較好

App在前臺時,彈出Dialog樣式的Activity

設置Activity樣式

<style name="AlertDialogActivityTheme" parent="Theme.AppCompat.Dialog">
  <item name="windowActionBar">false</item>
  <item name="android:windowFrame">@null</item>
  <item name="windowNoTitle">true</item> //去掉標題
  <item name="android:windowBackground">@android:color/transparent</item> //背景透明
  <item name="android:windowCloseOnTouchOutside">true</item> //設置觸摸彈框外面是否會消失
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowContentOverlay">@null</item>
  <item name="android:windowIsTranslucent">true</item>
</style>

AndroidManifest.xml

<activity
  android:name=".getui.AlertDialogActivity"
  android:theme="@style/AlertDialogActivityTheme">
</activity>

此處需要注意的是這里的Activity繼承的是AppCompatActivity,如果是繼承Activity,則一些屬性設置需要微調,比如去掉標題要改為

<item name="android:windowNoTitle">true</item>

以上設置以后還需要設置彈框的大小

public class AlertDialogActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_activity);
    
    //設置彈框大小,此處寬度為屏幕寬度減去160像素
    getWindow().setLayout(DeviceUtil.getDisplayParametersW(this)-160, ViewGroup.LayoutParams.WRAP_CONTENT);
    getWindow().setGravity(Gravity.CENTER);

    initView();
  }
}

App在后臺或是已經被銷毀

我們在接收到推送消息時都會彈出通知,這里只需要對常用的彈出通知方式進行微調一下

//關鍵的地方
PendingIntent contentIntent = PendingIntent.getActivities(context, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    //省略其他的一些設置
    .setContentIntent(contentIntent)
    //省略其他的一些設置
    
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify((int) System.currentTimeMillis() / 1000, notification);

上面關鍵的改動就在PendingIntent,里面的intents參數(shù)存放首頁Activity和目標Activity,比如

Intent[] intents = new Intent[2];
Intent intent_main = new Intent(getApplicationContext(), MainActivity.class);
Intent intent_target = new Intent(getApplicationContext(), TargetActivity.class);

intents[0] = intent_main;
intents[1] = intent_target;

通過以上的設置后,點擊通知欄就會打開TargetActivity,從TargetActivity返回后會打開MainActivity,而不會直接退出

需要注意的是,MainActivity需要設置啟動模式為singleInstance
AndroidManifest.xml

<activity
  android:name=".ui.main.MainActivity"
  android:launchMode="singleInstance" />

以上就是接收推送消息后的跳轉的一些內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

栖霞市| 莒南县| 开江县| 普宁市| 昌图县| 辽阳市| 津市市| 石城县| 东兰县| 福安市| 武功县| 巴马| 康定县| 罗山县| 梁山县| 黔西| 永城市| 延川县| 苗栗市| 灵台县| 桂平市| 武平县| 兴国县| 东丽区| 锦州市| 潍坊市| 黄山市| 监利县| 故城县| 昔阳县| 黄梅县| 德州市| 古浪县| 岐山县| 平江县| 辉县市| 合山市| 曲水县| 梁山县| 赤壁市| 象州县|