mBaaSを利用してAndroidアプリにpush通知を実装しようとしています、公式サイトによる基本的な使い方の通りに設定しmobile backendからpush通知を作成したのですが端末側で通知が表示されません。
試しにサンプルプログラムを入れてみた結果問題なく表示されたので通知の送信自体はうまくいっていると思われます。
データストアで確認したところInstallationも登録されています。
アプリ側のプログラムもサンプルプログラムとほぼ同じで、異なる点と言ったらライブラリのバージョンくらいなのですがバージョンによる違いで表示されないということはあるのでしょうか、
サンプルプログラムでは以下のようになっています
dependencies { compile fileTree(dir: 'libs', include: ['.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.google.code.gson:gson:2.3.1' compile files('libs/NCMB.jar') compile 'com.google.android.gms:play-services:8.1.0' }
java
1public class MainActivity extends ActionBarActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 //**************** APIキーの設定とSDKの初期化 ********************** 7 NCMB.initialize(this.getApplicationContext(), "YOUR_APPLICATION_KEY", "YOUR_CLIENT_KEY"); 8 9 final NCMBInstallation installation = NCMBInstallation.getCurrentInstallation(); 10 11 //GCMからRegistrationIdを取得しinstallationに設定する 12 installation.getRegistrationIdInBackground("ANDROID_SENDER_ID", new DoneCallback() { 13 //installation.getRegistrationIdInBackground("senderId", new DoneCallback() { 14 @Override 15 public void done(NCMBException e) { 16 if (e == null) { 17 installation.saveInBackground(new DoneCallback() { 18 @Override 19 public void done(NCMBException e) { 20 if (e == null) { 21 //保存成功 22 } else if (NCMBException.DUPLICATE_VALUE.equals(e.getCode())) { 23 //保存失敗 : registrationID重複 24 updateInstallation(installation); 25 } else { 26 //保存失敗 : その他 27 } 28 } 29 }); 30 } else { 31 //ID取得失敗 32 } 33 } 34 }); 35 36 setContentView(R.layout.activity_main); 37 } 38 39 40 41 public static void updateInstallation(final NCMBInstallation installation) { 42 43 //installationクラスを検索するクエリの作成 44 NCMBQuery<NCMBInstallation> query = NCMBInstallation.getQuery(); 45 46 //同じRegistration IDをdeviceTokenフィールドに持つ端末情報を検索する 47 query.whereEqualTo("deviceToken", installation.getDeviceToken()); 48 49 //データストアの検索を実行 50 query.findInBackground(new FindCallback<NCMBInstallation>() { 51 @Override 52 public void done(List<NCMBInstallation> results, NCMBException e) { 53 54 //検索された端末情報のobjectIdを設定 55 installation.setObjectId(results.get(0).getObjectId()); 56 57 //端末情報を更新する 58 installation.saveInBackground(); 59 } 60 }); 61 } 62 63 64 65 @Override 66 public boolean onCreateOptionsMenu(Menu menu) { 67 // Inflate the menu; this adds items to the action bar if it is present. 68 getMenuInflater().inflate(R.menu.menu_main, menu); 69 return true; 70 } 71 72 @Override 73 public boolean onOptionsItemSelected(MenuItem item) { 74 // Handle action bar item clicks here. The action bar will 75 // automatically handle clicks on the Home/Up button, so long 76 // as you specify a parent activity in AndroidManifest.xml. 77 int id = item.getItemId(); 78 79 //noinspection SimplifiableIfStatement 80 if (id == R.id.action_settings) { 81 return true; 82 } 83 84 return super.onOptionsItemSelected(item); 85 } 86}
開発中のプログラムでは以下の通りです
dependencies { // 省略 compile fileTree(dir: 'libs', include: ['.jar']) compile 'com.android.support:appcompat-v7:26.1.0' compile 'com.google.code.gson:gson:2.8.2' compile files('libs/NCMB.jar') compile 'com.google.android.gms:play-services-gcm:11.0.4' }
Java
1public class MainActivity extends AppCompatActivity { 2 private Intent intent; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 // mBaaS initialize 10 NCMB.initialize(this.getApplicationContext(), 11 "appKey", 12 "clientKey"); 13 14 // push 15 final NCMBInstallation installation = NCMBInstallation.getCurrentInstallation(); 16 17 //FCMからRegistrationIdを取得しinstallationに設定する 18 installation.getRegistrationIdInBackground("senderId", new DoneCallback() { 19 @Override 20 public void done(NCMBException e) { 21 if (e == null) { 22 installation.saveInBackground(new DoneCallback() { 23 @Override 24 public void done(NCMBException e) { 25 if(e == null){ 26 //保存成功 27 }else if(NCMBException.DUPLICATE_VALUE.equals(e.getCode())){ 28 //保存失敗 : registrationID重複 29 updateInstallation(installation); 30 }else if(NCMBException.DATA_NOT_FOUND.equals(e.getCode())) { 31 //保存失敗 : 端末情報の該当データがない 32 reRegistInstallation(installation); 33 }else { 34 //保存失敗 : その他 35 } 36 } 37 }); 38 } else { 39 //ID取得失敗 40 } 41 } 42 }); 43 44 45 // bottom navigation view 46 BottomNavigationView bottomNavigationView = (BottomNavigationView) 47 findViewById(R.id.navigation); 48 49 bottomNavigationView.setOnNavigationItemSelectedListener 50 (new BottomNavigationView.OnNavigationItemSelectedListener() { 51 @Override 52 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 53 Fragment selectedFragment = null; 54 switch (item.getItemId()) { 55 case R.id.home: 56 selectedFragment = new HomeFragment(); 57 break; 58 case R.id.like: 59 selectedFragment = new FavoriteFragment(); 60 break; 61 case R.id.settings: 62 selectedFragment = new SettingsFragment(); 63 } 64 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 65 transaction.replace(R.id.frame_layout, selectedFragment); 66 transaction.commit(); 67 return true; 68 } 69 }); 70 71 //Manually displaying the first fragment - one time only 72 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 73 transaction.replace(R.id.frame_layout, new HomeFragment()); 74 transaction.commit(); 75 } 76 77 public static void updateInstallation(final NCMBInstallation installation) { 78 79 //installationクラスを検索するクエリの作成 80 NCMBQuery<NCMBInstallation> query = NCMBInstallation.getQuery(); 81 82 //同じRegistration IDをdeviceTokenフィールドに持つ端末情報を検索する 83 query.whereEqualTo("deviceToken", installation.getDeviceToken()); 84 85 //データストアの検索を実行 86 query.findInBackground(new FindCallback<NCMBInstallation>() { 87 @Override 88 public void done(List<NCMBInstallation> results, NCMBException e) { 89 90 //検索された端末情報のobjectIdを設定 91 installation.setObjectId(results.get(0).getObjectId()); 92 93 //端末情報を更新する 94 installation.saveInBackground(); 95 } 96 }); 97 } 98 99 public static void reRegistInstallation(final NCMBInstallation installation) { 100 101 //objectIdと作成日・更新日のみ削除し、installationが再生成されるようにする 102 installation.setObjectId(null); 103 installation.remove("createDate"); 104 installation.remove("updateDate"); 105 106 //データストアの端末情報の再登録を実行 107 installation.saveInBackground(new DoneCallback() { 108 @Override 109 public void done(NCMBException e) { 110 if (e == null) { 111 //登録成功 112 } else { 113 //登録失敗 114 } 115 } 116 }); 117 } 118 119 void categoryMove(int position) { 120 switch (position) { 121 case 0: 122 intent = new Intent(this, TarotArticleActivity.class); 123 startActivity(intent); 124 break; 125 case 1: 126 intent = new Intent(this, BirthArticleActivity.class); 127 startActivity(intent); 128 break; 129 case 2: 130 intent = new Intent(this, NameArticleActivity.class); 131 startActivity(intent); 132 break; 133 } 134 } 135} 136
思い当たる原因等ございましたら教えていただきたいです、よろしくお願いします。
あなたの回答
tips
プレビュー