单击推送通知时如何在我的颤振应用程序中打开特定页面
Posted
技术标签:
【中文标题】单击推送通知时如何在我的颤振应用程序中打开特定页面【英文标题】:How do i open specific page in my flutter app when click the push notification 【发布时间】:2019-08-22 04:28:52 【问题描述】:点击推送通知时如何在我的 Flutter 应用中打开特定页面。我创建了一个 php 脚本来推送 FCM 推送通知,但它只是打开应用程序首页.....我希望推送通知带有日期并在我的应用程序中打开日历页面并显示通知的详细信息。
<?php
if(isset($_GET['send_notification']))
send_notification ();
function send_notification()
echo 'Hello';
define( 'API_ACCESS_KEY', 'Secret');
// $registrationIds = ;
#prep the bundle
$msg = array
(
'body' => 'App New Event Notification',
'title' => 'There is a new event added to the calendar',
);
$fields = array
(
'to' => $_REQUEST['token'],
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
echo $result;
curl_close( $ch );
?>
【问题讨论】:
【参考方案1】:这为我解决了问题。把它放在初始化 FirebaseApp 和 FirebaseMessaging 之后。
var _message = await FirebaseMessaging.instance.getInitialMessage();
if (_message != null)
// DO THE ROUTING
【讨论】:
【参考方案2】:在您的主页中,您可以处理 FCM 通知。
另外,请查看firebase 文档。
首先,您需要格式化 JSON。这就是我所遵循的。
"notification":
"title": "Some title",
"body": "Some text",
,
"data":
"title": "Some title",
"body": "Some text",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"sound": "default",
"status": "done",
"screen": "OPEN_PAGE1",
"extradata": "",
firebaseMessaging.configure(
onLaunch: (Map<String, dynamic> msg)
print("Called onLaunch");
print(msg);
,
onResume: (Map<String, dynamic> msg)
//(App in background)
// From Notification bar when user click notification we get this event.
// on this event navigate to a particular page.
print(msg);
// Assuming you will create classes to handle JSON data. :)
Notification ns =
Notification(title: msg['title'], body: msg['body']);
Data data = Data(
clickAction: msg['click_action'],
sound: msg['sound'],
status: msg['status'],
screen: msg['screen'],
extradata: msg['extradata'],
);
switch (data.screen)
case "OPEN_PAGE1":
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Page1()
),
);
break;
default:
break;
,
onMessage: (Map<String, dynamic> msg)
// (App in foreground)
// on this event add new message in notification collection and hightlight the count on bell icon.
// Add notificaion add in local storage and show in a list.
updataNotification(msg);
,
);
【讨论】:
嗯,这很好。我的理解基本上是我们需要在 main.dart 中添加这些代码,然后我们可以重定向到其他页面。如果我错了,请纠正我。 我还在苦苦挣扎。 :(***.com/questions/65166526/…以上是关于单击推送通知时如何在我的颤振应用程序中打开特定页面的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序被杀死时,无法在推送通知单击时导航到特定屏幕(Flutter android)