Android
Push Notifications
วิธีการแจ้งเตือนแบบพุชด้วย Cloud Code บน Parse
15 นาที
ส่งการแจ้งเตือนแบบพุช parse โดยใช้ cloud code บทนำ ส่วนนี้อธิบายวิธีการส่งการแจ้งเตือนแบบพุชโดยใช้ cloud code ผ่าน back4app นี่คือรูปลักษณ์ที่มันจะเป็น ในทุกเวลา คุณสามารถเข้าถึงโปรเจกต์ android ที่สร้างขึ้นโดยใช้บทเรียนนี้ได้ที่ https //github com/back4app/android cloud code push ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ เราต้องการ https //developer android com/studio/index html แอปที่สร้างขึ้นบน back4app หมายเหตุ ติดตาม https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอป parse บน back4app แอป android ที่เชื่อมต่อกับ back4app หมายเหตุ ติดตาม https //www back4app com/docs/android/parse android sdk เพื่อสร้างโปรเจกต์ android studio ที่เชื่อมต่อกับ back4app ติดตาม ขั้นตอนที่ 1 ถึง 5 ของ https //www back4app com/docs/android/push notifications/parse server push notifications อย่างระมัดระวังเพื่อกำหนดค่าการแจ้งเตือนแบบพุชให้กับแอปของคุณ อุปกรณ์ (หรือ https //developer android com/studio/run/managing avds html ) ที่ทำงานบน api level 27 หรือใหม่กว่า 1 ตั้งค่า android เพื่อรับการแจ้งเตือนแบบพุช ทุกแอป parse ที่ติดตั้งบนอุปกรณ์ที่ลงทะเบียนสำหรับการแจ้งเตือนแบบพุชมี การติดตั้ง การติดตั้ง ที่เกี่ยวข้อง แอป การติดตั้ง การติดตั้ง คือที่ที่คุณเก็บข้อมูลทั้งหมดที่จำเป็นสำหรับการกำหนดเป้าหมายการแจ้งเตือนแบบพุช ตัวอย่างเช่น ในแอปของคุณ คุณสามารถเก็บข้อมูลว่าทีมใดที่ผู้ใช้ของคุณสนใจเพื่อส่งข้อมูลอัปเดตเกี่ยวกับประสิทธิภาพของพวกเขา การบันทึก การติดตั้ง การติดตั้ง ก็จำเป็นสำหรับการติดตามเหตุการณ์เปิดแอปที่เกี่ยวข้องกับการแจ้งเตือนแบบพุช วิธีที่ง่ายที่สุดในการเริ่มส่งการแจ้งเตือนคือการใช้ช่องทาง ซึ่งช่วยให้คุณใช้โมเดลผู้เผยแพร่ ผู้ติดตามในการส่งการแจ้งเตือนแบบพุช อุปกรณ์เริ่มต้นโดยการสมัครสมาชิกช่องทางหนึ่งหรือมากกว่า และการแจ้งเตือนสามารถส่งไปยังผู้ติดตามเหล่านี้ในภายหลัง ช่องทางที่สมัครโดย การติดตั้ง การติดตั้ง จะถูกเก็บไว้ใน ช่องทาง ช่องทาง ของ การติดตั้ง การติดตั้ง เพื่อเริ่มทำงานกับการแจ้งเตือนแบบพุช ขั้นตอนต่อไปนี้จำเป็น หากคุณดาวน์โหลด https //github com/back4app/android cloud code push , อย่าลืมเปลี่ยนข้อมูลรับรองของคุณใน app/src/main/res/values/string xml app/src/main/res/values/string xml ไฟล์และ gcmsenderid gcmsenderid ที่คุณได้รับจาก firebase ใน androidmanifest xml androidmanifest xml ไฟล์ นำเข้าขึ้นอยู่ต่อไปนี้ 1 // ขึ้นอยู่กับ java 2 import java util arraylist ; 3 // ขึ้นอยู่กับ parse 4 import com parse parse ; 5 import com parse parseinstallation ; 2\ เริ่มต้น parse ด้วย parse initialize(this) parse initialize(this) 3\ สร้างอาร์เรย์ใหม่ของช่องและใส่ช่องที่คุณต้องการสมัคร ในตัวอย่างนี้ ช่อง ข่าว ข่าว ถูกสร้างขึ้น 4\ เพิ่ม gcmsenderid gcmsenderid ของคุณไปยังการติดตั้งของคุณ ซึ่งได้รับจาก https //console firebase google com/?pli=1 , ผ่านคำสั่ง installation put("gcmsenderid", "your firebase gcm sender id here") installation put("gcmsenderid", "your firebase gcm sender id here") เพื่อทราบว่าคุณจะได้รับกุญแจนั้นได้อย่างไร ให้ดูที่ ขั้นตอนที่ 1 ของ https //www back4app com/docs/android/push notifications/parse server push notifications 5\ เพิ่ม ช่องทาง ช่องทาง ลงใน การติดตั้ง การติดตั้ง ผ่านคำสั่ง installation put("channels", channels) installation put("channels", channels) 6\ บันทึกการติดตั้งลงในฐานข้อมูลของคุณผ่าน installation saveinbackground() installation saveinbackground() โค้ดต่อไปนี้จะดำเนินการตามขั้นตอนเหล่านี้ 1 parse initialize(this); 2 arraylist\<string> channels = new arraylist<>(); 3 channels add("news"); 4 parseinstallation installation = parseinstallation getcurrentinstallation(); 5 // don't forget to change the line below with the sender id you obtained at firebase 6 installation put("gcmsenderid", "your firebase gcm sender id here"); 7 installation put("channels", channels); 8 installation saveinbackground(); 2 สร้าง cloud code ของคุณ เพื่อทราบข้อมูลเพิ่มเติมเกี่ยวกับวิธีเริ่มต้นกับ cloud code ดูที่ https //www back4app com/docs/get started/cloud functions สร้าง js js ไฟล์เพื่อใส่ cloud code ของคุณ ในตัวอย่างนี้จะมีการสร้าง main js main js ไฟล์ กำหนดฟังก์ชัน cloud โดยใช้ parse cloud define parse cloud define , เพื่อเรียกการแจ้งเตือนแบบพุช ในตัวอย่างนี้ ฟังก์ชันนี้เรียกว่า parse push send parse push send จำเป็นต้องใช้ master key ในกระบวนการนี้ โค้ดต่อไปนี้จะดำเนินการตามขั้นตอนเหล่านี้ parse server 3 x //main js 1 parse cloud define("pushsample", (request) => { 2 3 return parse push send({ 5 channels \["news"], 6 data { 7 title "hello from the cloud code", 8 alert "back4app rocks!", 9 } 10 }, { usemasterkey true }); 11 }); parse server 2 x //main js 1 parse cloud define("pushsample", function (request, response) { 2 parse push send({ 3 channels \["news"], 4 data { 5 title "hello from the cloud code", 6 alert "back4app rocks!", 7 } 8 }, { 9 success function () { 10 // push was successful 11 response success("push sent"); 12 console log("success push sent"); 13 }, 14 error function (error) { 15 // push was unsucessful 16 response error("error with push " + error); 17 console log("error " + error); 18 }, 19 usemasterkey true 20 }); 21 }); 3 อัปโหลดไปยัง cloud code ไปที่แอปของคุณที่ https //www back4app com/ และคลิกที่ แดชบอร์ด แดชบอร์ด ค้นหา cloud code cloud code และคลิกที่ ฟังก์ชัน & โฮสติ้งเว็บ ฟังก์ชัน & โฮสติ้งเว็บ มันดูเหมือนแบบนี้ 3\ อัปโหลดหรือสร้างไฟล์ใหม่ (คุณยังสามารถแก้ไข main js main js ไฟล์ปัจจุบันโดยตรงในเบราว์เซอร์) จากนั้นคลิกที่ deploy deploy ตามที่แสดงที่นี่ 4 เรียกใช้ cloud code จากแอป android นำเข้าข้อกำหนดต่อไปนี้ 1 // การพึ่งพา java 2 import java util hashmap ; // นี่รวมถึงวัตถุ hasmap ที่ฟังก์ชัน cloud 4 ต้องเรียก 3 // การพึ่งพา parse 4 import com parse functioncallback ; 5 import com parse parsecloud ; 6 import com parse parseexception ; 2\ เรียกใช้ parsecloud callfunctioninbackground parsecloud callfunctioninbackground บน pushsample pushsample ฟังก์ชันคลาวด์ 1 final hashmap\<string, string> params = new hashmap<>(); 2 // calling the cloud code function 3 parsecloud callfunctioninbackground("pushsample", params, new functioncallback\<object>() { 4 @override 5 public void done(object response, parseexception exc) { 6 if(exc == null) { 7 // the function was executed, but it's interesting to check its response 8 alertdisplayer("successful push","check on your phone the notifications to confirm!"); 9 } 10 else { 11 // something went wrong 12 toast maketext(mainactivity this, exc getmessage(), toast length long) show(); 13 } 14 } 15 }); วิธีการ alertdisplayer alertdisplayer ที่ใช้ในตัวอย่างข้างต้นคือ 1 private void alertdisplayer(string title ,string message ){ 2 alertdialog builder builder = newalertdialog builder (mainactivity this ) 3 settitle ( title ) 4 setmessage ( message ) 5 setpositivebutton ( "ok" , new dialoginterface onclicklistener () { 6 @override 7 public void onclick(dialoginterface dialog , int which ) { 8 dialog cancel (); 9 } 10 }); 11 alertdialog ok = builder create (); 12 ok show (); 13 } 3\ ทดสอบว่าการแจ้งเตือนแบบพุชถูกส่งหรือไม่โดยการเรียกใช้ฟังก์ชันข้างต้นในขณะที่อุปกรณ์เปิดอยู่ 5 เรียกใช้ cloud code จาก rest api rest api ให้วิธีที่รวดเร็วและง่ายในการทดสอบว่าฟังก์ชัน cloud ของคุณทำงานหรือไม่ เพียงใช้โค้ดด้านล่างในเทอร์มินัลหรือพรอมต์คำสั่งของคุณ คลิกที่นี่เพื่อทราบข้อมูลเพิ่มเติมเกี่ยวกับวิธีเริ่มต้นใช้งานบรรทัดคำสั่งใน https //www digitalocean com/community/tutorials/an introduction to the linux terminal , https //blog teamtreehouse com/introduction to the mac os x command line หรือ https //www bleepingcomputer com/tutorials/windows command prompt introduction/ curl x post h "x parse application id your app id here" \\ h "x parse rest api key your rest api key here" \\ h "content type application/json" \\ d '{ // ใส่พารามิเตอร์ฟังก์ชันที่นี่ในรูปแบบ json }' \\ https //parseapi back4app com/functions/pushsample ในการทดสอบการแจ้งเตือนแบบพุช เพียงใช้โค้ด rest ขณะที่อุปกรณ์เปิดอยู่ เสร็จสิ้น! ในขั้นตอนนี้ คุณสามารถส่งการแจ้งเตือนแบบพุชโดยใช้ cloud code ผ่าน back4app!