2016-09-23 91 views
2

我將Firebase通知集成到了我的應用程序中,但我想發送一個通知,以打開特定活動並執行我計劃要做的事情,而不僅僅是打開該應用程序。就像通知會推動用戶點擊Google Play商店一樣。從Firebase通知中打開特定活動

我看到了一個代碼Firebase console: How to specify click_action for notifications,但我在初始化變量cls時出錯。我試圖通過定義cls = null來解決,以清除錯誤。它無法打開我指定的活動使用click_action

public class ClickActionHelper { 
public static void startActivity(String className, Bundle extras, Context context){ 
Class cls=null; 
try { cls = Class.forName(className); 
} 
catch(ClassNotFoundException e){ 
//means you made a wrong input in firebase console 
} 
Intent i = new Intent(context, cls); 
i.putExtras(extras); context.startActivity(i); 
}  
} 

請問我有什麼問題嗎?我如何得到這個工作?

回答

5

如果您想要打開應用並執行特定操作[同時背景],請在通知有效內容中設置click_action並將其映射到要啓動的活動中的意圖過濾器。例如,設置click_action到OPEN_ACTIVITY_1觸發像一個意圖過濾以下:

正如FCM文件建議,要求後端在這樣的形式發送JSON數據,

{ 

    "to":"some_device_token", 

    "content_available": true, 
    "notification": { 
    "title": "hello", 
    "body": "yo", 
    "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity 
    }, 

"data": { 
"extra":"juice" 
} 
} 

,並在您mainfest文件爲您的活動添加意圖過濾器,如下

<intent-filter> 
    <action android:name="OPEN_ACTIVITY_1" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

當您單擊該通知,它會打開應用程序,直接進入您在click_action定義,在這種情況下,「OPEN_ACTIVTY_1」活動。而這一活動中,你可以得到的數據是:

Bundle b = getIntent().getExtras();// add these lines of code to get data from notification 
String someData = b.getString("someData"); 

退房下面的鏈接以獲得更多幫助:

Firebase FCM notifications click_action payload

Firebase onMessageReceived not called when app in background

Firebase console: How to specify click_action for notifications