Android
Push Notifications
Cloud Codeを使ったParseプッシュ通知: 開発者向けガイド
15 分
cloud codeを使用してparseプッシュ通知を送信する はじめに このセクションでは、back4appを通じてcloud codeを使用してプッシュ通知を送信する方法を説明します。 これがどのように見えるかです いつでも、このチュートリアルで構築された完全な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 を参照して、back4appでparseアプリを作成する方法を学んでください。 back4appに接続されたandroidアプリ。 注意 次の https //www back4app com/docs/android/parse android sdk を参照して、back4appに接続されたandroid studioプロジェクトを作成してください。 次の ステップ 1 から 5 の https //www back4app com/docs/android/push notifications/parse server push notifications を注意深く実行して、アプリにプッシュ通知を設定してください。 apiレベル27以上で動作するデバイス(または https //developer android com/studio/run/managing avds html )。 1 プッシュを受信するためのandroidの設定 プッシュ通知に登録されたデバイスにインストールされたすべてのparseアプリケーションには、関連する インストール インストール オブジェクトがあります。 インストール インストール オブジェクトは、プッシュ通知をターゲットにするために必要なすべてのデータを保存する場所です。たとえば、アプリ内で、ユーザーの一人が興味を持っているチームを保存して、パフォーマンスに関する更新を送信することができます。 インストール インストール オブジェクトを保存することは、プッシュ関連のアプリオープンイベントを追跡するためにも必要です。 通知を送信し始める最も簡単な方法は、チャネルを使用することです。これにより、プッシュを送信するためのパブリッシャー サブスクライバーモデルを使用できます。デバイスは1つ以上のチャネルにサブスクライブし、通知は後でこれらのサブスクライバーに送信できます。特定の インストール インストール にサブスクライブされたチャネルは、 チャネル チャネル フィールドに保存されます。 インストール インストール オブジェクトの。 プッシュ通知を使用するには、次の手順が必要です もし私たちの 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 initialize(this) parse initialize(this) でparseを初期化します。 3\ チャンネルの新しい配列を作成し、購読したいチャンネルを追加します。この例では、 news news チャンネルが作成されます。 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\ インストールをバックグラウンドで保存する インストールをバックグラウンドで保存する ことで、データベースにインストールを保存します。 次のコードはこれらのステップを実行します 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 を見てください。 https //www back4app com/docs/get started/cloud functions 。 cloud codeを入れるための js js ファイルを作成します。この例では、 main js main js ファイルが作成されます。 プッシュ通知を呼び出すために、 parse cloud define parse cloud define を使用してcloud関数を定義します。この例では、この関数は parse push send parse push send と呼ばれます。 この操作では、 マスターキー を使用する必要があります。 以下のコードはこれらのステップを実行します 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/ をクリックします ダッシュボード ダッシュボード 次の クラウドコード クラウドコード を見つけて 関数とウェブホスティング 関数とウェブホスティング をクリックします。これは次のようになります 3\ 新しいファイルをアップロードまたは作成します(現在の main js main js ファイルをブラウザ上で直接編集することもできます)。次に、 デプロイ デプロイ をクリックします。ここに示すように 4 androidアプリからクラウドコードを呼び出す 次の依存関係をインポートします 1 // java 依存関係 2 import java util hashmap ; // これは、4つのクラウド関数が呼び出す必要があるhasmapオブジェクトを含みます 3 // 依存関係の解析 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 rest apiからcloud codeを呼び出す 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コードを使用してください。 完了しました! この段階で、back4appを通じてcloud codeを使用してプッシュ通知を送信できます!