Android 接收推送消息跳轉(zhuǎn)到指定頁面的方法
問題的提出
本次接入的是個推,其他家的推送沒有研究過,思路應(yīng)該是類似的
App在前臺,這個時候需要彈出一個對話框,提醒用戶有新的消息,是否要查看,查看的話跳轉(zhuǎn)到指定頁面
App在后臺,或是App進(jìn)程已經(jīng)被系統(tǒng)回收,收到推送后App進(jìn)程會被個推拉起。這時候要展示通知,點(diǎn)擊通知欄打開App并跳轉(zhuǎn)到目標(biāo)頁面,關(guān)閉目標(biāo)頁面后需要返回到應(yīng)用首頁,而不是直接推出App
實(shí)現(xiàn)思路
App在前臺時,彈出Dialog提醒用戶有新消息,但是最新版的個推文檔接收推送消息是繼承IntentService,無法獲取彈出Dialog所需要的Context(注意不能用getApplicationContext()),所以采用Dialog樣式的Activity來實(shí)現(xiàn)
App在后臺時,如果直接在PendingIntent中傳目標(biāo)Activity的Intent,則在退出目標(biāo)Activity時會直接退出應(yīng)用,感覺像是閃退了一樣;如果是跳轉(zhuǎn)到首頁,然后在首頁中檢測是否是由點(diǎn)擊通知進(jìn)入應(yīng)用的來進(jìn)行跳轉(zhuǎn),這樣的話首頁就會閃屏。綜上方法都不是很理想,一個比較好的解決方案是給PendingIntent傳遞一個Intent數(shù)組,分別放置目標(biāo)Activity和首頁,這樣效果比較好
App在前臺時,彈出Dialog樣式的Activity
設(shè)置Activity樣式
<style name="AlertDialogActivityTheme" parent="Theme.AppCompat.Dialog"> <item name="windowActionBar">false</item> <item name="android:windowFrame">@null</item> <item name="windowNoTitle">true</item> //去掉標(biāo)題 <item name="android:windowBackground">@android:color/transparent</item> //背景透明 <item name="android:windowCloseOnTouchOutside">true</item> //設(shè)置觸摸彈框外面是否會消失 <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,則一些屬性設(shè)置需要微調(diào),比如去掉標(biāo)題要改為
<item name="android:windowNoTitle">true</item>
以上設(shè)置以后還需要設(shè)置彈框的大小
public class AlertDialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_activity);
//設(shè)置彈框大小,此處寬度為屏幕寬度減去160像素
getWindow().setLayout(DeviceUtil.getDisplayParametersW(this)-160, ViewGroup.LayoutParams.WRAP_CONTENT);
getWindow().setGravity(Gravity.CENTER);
initView();
}
}
App在后臺或是已經(jīng)被銷毀
我們在接收到推送消息時都會彈出通知,這里只需要對常用的彈出通知方式進(jìn)行微調(diào)一下
//關(guān)鍵的地方
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)
//省略其他的一些設(shè)置
.setContentIntent(contentIntent)
//省略其他的一些設(shè)置
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify((int) System.currentTimeMillis() / 1000, notification);
上面關(guān)鍵的改動就在PendingIntent,里面的intents參數(shù)存放首頁Activity和目標(biāo)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;
通過以上的設(shè)置后,點(diǎn)擊通知欄就會打開TargetActivity,從TargetActivity返回后會打開MainActivity,而不會直接退出
需要注意的是,MainActivity需要設(shè)置啟動模式為singleInstance
AndroidManifest.xml
<activity android:name=".ui.main.MainActivity" android:launchMode="singleInstance" />
以上就是接收推送消息后的跳轉(zhuǎn)的一些內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android?studio?利用共享存儲進(jìn)行用戶的注冊和登錄驗(yàn)證功能
- Android Studio實(shí)現(xiàn)QQ的注冊登錄和好友列表跳轉(zhuǎn)
- Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊
- Android Studio連接MySql實(shí)現(xiàn)登錄注冊(附源代碼)
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實(shí)現(xiàn)
- Android使用Intent顯示實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android啟動頁面定時跳轉(zhuǎn)的三種方法
- Android實(shí)現(xiàn)外部喚起應(yīng)用跳轉(zhuǎn)指定頁面的方法
- Android Studio實(shí)現(xiàn)注冊頁面跳轉(zhuǎn)登錄頁面的創(chuàng)建
相關(guān)文章
Android自定義ViewGroup實(shí)現(xiàn)絢麗的仿支付寶咻一咻雷達(dá)脈沖效果
這篇文章主要介紹了Android自定義ViewGroup實(shí)現(xiàn)絢麗的仿支付寶咻一咻雷達(dá)脈沖效果的相關(guān)資料,需要的朋友可以參考下2016-10-10
Kotlin協(xié)程啟動createCoroutine及創(chuàng)建startCoroutine原理
這篇文章主要為大家介紹了Kotlin協(xié)程啟動createCoroutine及創(chuàng)建startCoroutine原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)
這篇文章主要介紹了Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
Android Studio搜索功能(查找功能)及快捷鍵圖文詳解
這篇文章主要介紹了Android Studio搜索功能(查找功能)及快捷鍵圖文詳解,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-12-12
Android使用模板生成支持手機(jī)直接查看的Word文檔
這篇文章主要為大家詳細(xì)介紹了Android 使用模板生成Word文檔,支持手機(jī)直接查看word,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android使用MediaRecorder類實(shí)現(xiàn)視頻和音頻錄制功能
Android提供了MediaRecorder這一個類來實(shí)現(xiàn)視頻和音頻的錄制功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧2018-07-07
Android sqlite設(shè)置主鍵自增長的方法教程
這篇文章主要給大家介紹了關(guān)于Android sqlite設(shè)置主鍵自增長的方法教程,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
Android使用注解進(jìn)行代碼檢查的實(shí)現(xiàn)方法
這篇文章主要介紹了Android如何使用注解進(jìn)行代碼檢查,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

