2016-10-27 29 views
0

FCM通知コンソールからテストのために私のアプリケーションにプッシュ通知を取得しようとしていますが、シングルデバイスでプッシュ通知を開始するトークンが取得されません。 ユーザセグメント上では、(ユーザセグメントオプションを使用してプッシュ通知を取得していますが)単一デバイスでは機能していません。FCMを使用してトークンを取得していません

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

    private static final String TAG = "MyFirebaseIIDService"; 

    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 

     sendRegistrationToServer(refreshedToken); 
    } 

    private void sendRegistrationToServer(String token) { 

    } 
} 

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     } 

     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
      String msg = remoteMessage.getNotification().getBody(); 
      sendNotification(msg); 
     } 
    } 

    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.questionshield) 
      .setContentTitle("FCM Message") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

build.gradle(モジュール)

apply plugin: 'com.android.application' 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:support-v4:23.2.0' 
    compile 'com.android.support:support-v4:23.2.0' 
    compile 'com.android.support:appcompat-v7:23.2.0' 
    compile 'com.android.support:cardview-v7:23.2.0' 
    compile 'com.android.support:recyclerview-v7:23.2.0' 
    compile files('glide-3.7.0.jar') 
    compile 'com.android.support:design:23.2.0' 
    compile 'com.google.android.gms:play-services-ads:9.0.0' 
    compile 'com.google.android.gms:play-services-analytics:9.0.0' 
    compile 'com.google.firebase:firebase-core:9.0.0' 
    compile 'com.google.firebase:firebase-messaging:9.0.0' 
} 
apply plugin: 'com.google.gms.google-services' 

マニフェストファイル

<service android:name=".MyFirebaseMessagingService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
    </intent-filter> 
</service> 
<service 
    android:name=".MyFirebaseInstanceIDService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
    </intent-filter> 
</service> 
+0

あなたは 'クラスパスに置く必要がありますでしょう「com.google.gms:グーグルのサービスを:3.0.0」 'あなたのプロジェクトの依存関係の中で – ZeroOne

+0

ログには何も表示されていませんか? –

答えて

0

あなたはアプリレベルの依存関係にラインの下に追加しましたか?

dependencies { 
    // ... 
    classpath 'com.google.gms:google-services:3.0.0' 
} 

私は同じ問題に直面していました。アプリケーションレベルbuild.gradleでこの行を追加した後、私はトークンを受け取った。

Here詳細を見るためのリンクです。

+0

はい。私はちょうど相対的なものを投稿しました –

0

はインスタンスIDトークンが

を更新しているのであればそれだけで呼び出す呼び出さ

https://developers.google.com/cloud-messaging/android/android-migrate-fcm

onTokenRefresh() 

によると、打ち上げ活動に

FirebaseMessaging.getInstance().subscribeToTopic("message"); 
    String token = FirebaseInstanceId.getInstance().getToken(); 
    Log.d(TAG, "FCM Token: "+token); 
+0

エラー:java.lang.IllegalStateException:名前[DEFAULT]のFirebaseAppが存在しません。 –

+0

firebaseコンソールでappを作成しましたか?サーバーサイドコードとは何ですか? –

+0

はい、私はそれを作成して、それをコンソールにしようとしています。 –

-1

をこれらの行を追加してみてくださいあなたのデバイスに初めてアプリケーションをインストールするとき。

だから私はアプリを手動でアンインストールして、間違いなく再び

を実行してみてくださいお勧め、あなたはTOKEN

+0

'onTokenRefresh()'はあなたのデバイスに初めてアプリケーションをインストールするときにのみ呼び出されます*。 [こちら](https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceIdService.html#onTokenRefresh())をご覧ください。 –

関連する問題