monacaアプリでアプリを制作していますが、プッシュ通知のON-OFFをアプリ内で設定できるようにしたいと考えています。
ON-OFFのスイッチはonsenUIのons-switchコンポーネントで表示できたのですが、うまく動作しません。
isChecked()メソッドで、プッシュ通知がONになっているときはプッシュ通知のjavascript部分を読み込むという仕組みです。
プッシュ通知はFCMを利用しています。
実際のコードはこちらになります。
javascript
1if (isChecked()){ 2 3window.FirebasePlugin.grantPermission(function(){ 4 console.log("Permission is granted for iOS"); 5 }, function(error){ 6 alert(error); 7 }); 8 9document.addEventListener("deviceready", function(){ 10 window.FirebasePlugin.getToken(function(token) { 11 // save this server-side and use it to push notifications to this device 12 if(token == null){ 13 window.FirebasePlugin.onTokenRefresh(function(token){ 14 // onTokenRefresh() でトークンを取得時の処理 15 console.log(token); 16 }); 17 } else { 18 // getToken() でトークンを取得した時の処理 19 console.log(token); 20 } 21 22 }, function(error) { 23 console.error(error); 24 }); 25 26 27 // Get notified when a token is refreshed 28 window.FirebasePlugin.onTokenRefresh(function(token) { 29 // save this server-side and use it to push notifications to this device 30 console.log("Refresh to get new token: " + token); 31 }, function(error) { 32 alert(error); 33 }); 34 35 // Get notified when the user opens a notification 36 window.FirebasePlugin.onNotificationOpen(function(notification) { 37 console.log(JSON.stringify(notification)); 38 console.log("The notification is open!"); 39 }, function(error) { 40 console.error(error); 41 }); 42}, false); 43};
間違えている部分をご指摘していただければ幸いです。
よろしくお願いいたします。
あなたの回答
tips
プレビュー