android - Cordova android fcm插件jquery php点击背景通知
问题描述
我使用 cordova 和 Google FMC 通知插件制作应用程序,并且在应用程序处于前台之前一切正常。
//MY index.php
<div id="load"></div>
<script>
FCMPlugin.onNotification(function(data){
alert( JSON.stringify(data) );
// I DONT UNDERSTAND THIS SECTION ???
// WHAT I SCHOULT DO HERE???
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert( JSON.stringify(data) );
// I RECIVED THE DATA ON "CLOSED" APP! GET VARS AND LOAD FILE INTO div id="load" WITH AJAX BASED ON KEY SENDTH FROM FIREBASE CONSOLE
// EXAMPLE KEY = MESSAGE = 10890
// $AJAX LOAD FILE MESSAGE ?ID=10890
// CLEAR NOTIFICATIONS
//example ajax
var message= 10890; // GET FROM CORDOVA NOTIFICATION
var dataString = '& message=' + message;
$.ajax({
type: "POST",
url: "https://mywebsite.com/LOAD.php",
data: dataString,
success: function(response) {
$('#info').html(response);
}
});
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert( JSON.stringify(data) );
// I RECIVED THE DATA ON OPEN APP! GET VARS AND LOAD FILE INTO div id="load" WITH AJAX BASED ON KEY SENDTH FROM FIREBASE CONSOLE
// EXAMPLE KEY = MESSAGE = 10890
// $AJAX LOAD FILE MESSAGE ?ID=10890
// CLEAR NOTIFICATIONS
// HOW TO GET VAR FROM alert( JSON.stringify(data) ); ??
//example ajax
var message= 10890; // GET FROM CORDOVA NOTIFICATION
var dataString = '& message=' + message;
$.ajax({
type: "POST",
url: "https://mywebsite.com/LOAD.php",
data: dataString,
success: function(response) {
$('#info').html(response);
}
});
}
});
</script>
如果有人可以帮助解决这个谜团,那真是太好了。我希望它可以帮助其他像我这样不专业的人。我仅将应用程序用作包装器并加载外部 .php 应用程序可以很好地处理我需要的所有功能,这是我需要解决的唯一问题。
谢谢,对不起,我是新手
解决方案
发送通知的问题
<?php
$url = "https://fcm.googleapis.com/fcm/send";
$token = "xxxx";
$serverKey = 'xxx';
$topic = 'mytopic';
$title = "title";
$body = "Message.";
$img = 'https://url.com/images/img.jpg';
$data = array('USR_id' => '1000',
'USR_name' => 'Name',
'MSG_id' => '1004',
'SEN_id' => '1001',
'USR_level' => 'user');
$android = array('senderID' => 'xxxxxxxxxxxx',
'sound' => true,
'vibrate' => true,
'clearNotifications' => true,
'forceShow' => true);
$notification = array('title' =>$title,
'body' => $body,
'image' => $img,
'sound' => 'audio.mp3',
'color' => '#F6C02B',
'click_action' => 'FCM_PLUGIN_ACTIVITY',
'badge' => '1');
$arrayToSend = array('to' => '/topics/' . $topic,
'notification' => $notification,
'data' => $data,
'android' => $android,
'priority' => 'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
//die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
?>
在我用 php 发送它之后完美
推荐阅读
- sql - 如何使用 HQL 替换 Hive 数据库中的行
- java - 可观察订阅未启动事件序列
- java - Java - 使用单个表情符号读取 UTF-8 文件
- swift - 如何更新 Swift5 + Xcode11.6 的工具栏?
- database - 如何使用 Bigquery 中的字段历史表重新创建旧快照
- c# - WebClient.DownloadString 抛出空引用异常
- python - 使用 for 循环或 str 格式绕过 Scrapy 爬网列表
- assembly - 386的“D位”中的“D”代表什么?
- java - 在 Chrome 浏览器中接受/忽略 SSL 证书
- sql - 按某些列 SQL 旋转(转置)