问题描述
我正在使用以下代码向设备发送推送通知.
I am sending push notification to devices using below code.
var nTitle = "New message from " + $rootScope.clients_firstName;
var to = "DeviceToken is here";
var notification = {
'title': nTitle,
//'body': 'Click here to more details...',
'icon': 'firebase-logo.png',
'click_action': 'openapp'
};
var key = 'your key';
fetch('https://fcm.googleapis.com/fcm/send', {
'method': 'POST',
'headers': {
'Authorization': 'key=' + key,
'Content-Type': 'application/json'
},
'body': JSON.stringify({
'notification': data,
'to': to
})
}).then(function(response) {
//console.log("res ", response);
}).catch(function(error) {
console.error("err ", error);
});
推送通知在设备上成功发送.
push notification sends on device successfully.
但是当我点击通知时,特定页面应该是打开的.
But when I click on notification the specific page should be open.
例如 'click_action' : 'openapp/somepage'
然后当我点击推送通知时'somepage'应该是打开的.
Then the 'somepage' should be open when I click on pushnotification.
AndroidManifest.xml
<activity android:name="MainActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="openapp" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
我的项目结构
--platforms
--plugins
--www
--app
--about
--about.html
--about.ctrl.js
--product
--product.html
--product.ctrl.js
--css
--js
--lib
index.html
如果我点击通知并想打开产品或关于页面,那么我必须做什么?
If I click on notification and I want to open product or about page then what I have to do?
这里有什么问题?请告诉我正确的解决方案.
What is wrong here? Please tell me the proper solution.
推荐答案
要使通知在您的应用在后台时可点击,您需要在通知负载中使用 click_action
属性.
For notifications to be clickable when your app is in the background, you need the click_action
attribute in your notification payload.
请检查这个部分.
此外,当您定义click_action
属性时,您还需要在 中对应一个
属性您希望启动的活动的代码>.
Also, when you define the click_action
attribute, you will also need a corresponding <action>
attribute in the <intent-filter>
of the activity that you wish to launch.
这个视频 非常详细地解释了这一点.
This video explains it in quite a detailed manner.
不过,请注意,如果您从 Firebase 控制台发送通知,则无法设置 click__action
属性.只有当您从自己的管理服务器或使用 Firebase Cloud Functions 发送通知时才能这样做.
Though, please note that you can not set the click__action
attribute if you're sending notifications from the Firebase Console. You can only do so if you send a notification from your own Admin server or using Firebase Cloud Functions.
最后,在启动的活动中,您可以使用 data 属性设置额外的 Data
(也显示在我上面链接的同一个文档中).当您通过单击通知启动应用程序时,您可以使用 getIntent()
获取通知数据.查看此答案,了解有关如何执行此操作的更多详细信息.
Lastly, in the activity that is launched, you can set additional Data
using the data attribute ( also shown in the same doc that I linked above ). And when you launch your app by clicking on a notification, you can obtain the notification data using getIntent()
. Check out this answer for more details on how to do that.
问题更新后-
不要将 放在
MainActivity
中,而是将过滤器放在活动的 标签中单击通知时要打开的那个.一个例子如下:-
Instead of putting that <intent-filter>
in MainActivity
, put the filter in the <activity>
tag of the activity that you want to open when the notification is clicked. An example is the following :-
<activity android:name="ProductDetailsActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="openapp" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
现在您指定的活动已打开,您可以使用 getIntent()
从通知的 data
部分获取信息.
Now that your specified activity opens, you can get information from the data
part of your notification using getIntent()
.
例如,如果您的通知负载具有以下结构,
For example, if your notification payload has the following structure,
payload = {
notification: {
title: `You ordered a new product`,
click_action : 'HANDLE_NOTIFICATION',
},
data : {
product_id : 'ABC98292',
type : `Clothes`,
product_name : 'Cotton spring shirt'
}
};
然后您可以使用 getIntent().getStringsExtra("product_id")
等从通知中获取 product_id
.
Then you can get the product_id
from the notification using getIntent().getStringsExtra("product_id")
and so on.
这样,您将打开所需的 Activity,并可以使用从通知中获取的相关详细信息填充它,而无需使用任何其他框架.
This way, you'll be opening the required activity, and can populate it with the relevant details obtained from your notification without using any additional frameworks.
这篇关于使用 firebase api 打开特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!