OneSignal在Web通知上发送js变量

时间:2018-12-13 16:40:51

标签: javascript php push-notification notifications onesignal

有人知道如何在Web通知内容上发送js变量吗?

我正在尝试以下代码,但无法正常工作,并且得到的内容为空。

我将PHP变量添加到JS变量notifyText中,内容在那里,但通知内容中什么都没有显示。

<script type="text/javascript">
                                var noticeText = "<?php echo $noticeTitle; ?>";
				var settings = {
				  "async": true,
				  "crossDomain": true,
				  "url": "https://onesignal.com/api/v1/notifications",
				  "method": "POST",
				  "headers": {
				    "cookie": "__cfduid=d7e029aceb39ed3bc3b64849b5358bf3a1544538404",
				    "authorization": "Basic MzVmZGQ5ZWQtNGI5OC00YTU5LWFhOGUtMTZhZWVmMzg3MGIx",
				    "content-type": "application/json"
				  },
				  "processData": false,
				  "data": 
'{"app_id": "dc44e367-f59b-41fd-a7c9-1b25cfd8cb66","included_segments": ["All"],"headings": {"en": "Hey {{ first_name }} {{ last_name }}", "tr": "Turkish {{ first_name }} {{ last_name }}"},"contents": {"en": "{{ noticeText }}", "tr": "Turkish Content"}}'
				}
				
				$.ajax(settings).done(function (response) {
				  console.log(response); 
				});
			</script>

1 个答案:

答案 0 :(得分:0)

直接php echo最终奏效了,诀窍是让它在“”中变得像魔术一样起作用