2017-07-11 17 views
0

FCMトークンが取得されません。私のログイン画面でFCMトークンIDが生成されない

私はトークンを取得しておりません。この

startService(new Intent(this, FetchNewRefreshToken.class)); 
     startService(new Intent(this, ContCreateTokenService.class)); 

私のマニフェスト

<service android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 
     <service android:name=".FirebaseIdService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service> 
     <service android:name=".ContCreateTokenService" /> 
     <service android:name=".FetchNewRefreshToken" /> 



public class ContCreateTokenService extends Service { 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Intent serviceIntent = new Intent(this, MyFirebaseMessagingService.class); 
     startService(serviceIntent); 
     return START_REDELIVER_INTENT; 
    } 
} 



public class FetchNewRefreshToken extends IntentService { 
    /** 
    * Creates an IntentService. Invoked by your subclass's constructor. 
    * 
    * @param name Used to name the worker thread, important only for debugging. 
    */ 

    public static final String TAG = FetchNewRefreshToken.class.getSimpleName(); 


    public FetchNewRefreshToken() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     try { 

      // Resets Instance ID and revokes all tokens. 
      FirebaseInstanceId.getInstance().deleteInstanceId(); 


      // Now manually call onTokenRefresh() 
      Log.d(TAG, "Getting new token"); 
      String token= FirebaseInstanceId.getInstance().getToken(); 
      final Intent intents = new Intent("tokenReceiver"); 
      // You can also include some extra data. 
      final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this); 
      intents.putExtra("token", token); 
      broadcastManager.sendBroadcast(intents); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 


public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "FCM Service"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO: Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 



      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
      sendNotification("text"); 
     } 
// Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 
    } 

    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 , intent, 
       PendingIntent.FLAG_ONE_SHOT); 
     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_notifications_black_24dp) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 
     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 

    } 

} 

マイmainActivityクラス

@Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      LocalBroadcastManager.getInstance(this).registerReceiver(tokenReceiver, 
        new IntentFilter("tokenReceiver")); 
    } 
BroadcastReceiver tokenReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String token = intent.getStringExtra("token"); 
      //Log.d("token",token); 
      // String token=FirebaseInstanceId.getInstance().getToken(); 
      if(token != null) 
      { 
       //send token to your server or what you want to do 
       FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
       Log.d("token",token); 
       Firebase reference1 = new Firebase("https://fuudful-1a15c.firebaseio.com/users/" +user.getUid()); 
       Map<String, String> map = new HashMap<String, String>(); 
       map.put("registrationtoken", token); 
       reference1.push().setValue(map); 
      } 

     } 
    }; 

を呼び出しています、通知もありませんデータベースには挿入されません。

私にこれを手伝ってもらえますか?

答えて

0

インテントサービスを作成する代わりに、トークンを生成する独自のサービスを使用できますか。それをあなたの好みに保存し、いつでも好きなときに使うことができます。あなたのAndroidManifest.xmlファイルにこの行を入れ

public class TokenService extends FirebaseInstanceIdService { 

@Override 
public void onTokenRefresh() { 
    // Get updated InstanceID token. 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.w("notification", refreshedToken); 
    //store token into preference 
    } 
} 

<service 
     android:name=".TokenService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service> 
+0

は私が意図 – asifa

+0

を作成したYのthatsを働いていないのは、あなたの携帯デビックであることのマニフェストファイルでサービスを定義しますGoogle Playサービスを利用していますか? –

+0

はいGoogleのプレイサービスアプリ – asifa

1

チェックGooglePlayserviceVersion古いバージョンがインストールされている場合、それは確認してくださいGoogleはサービスのバージョンを再生するためのfirebase

方法で問題を作成します。

private boolean checkPlayServices() { 
 
     GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); 
 
     int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); 
 
     if (resultCode != ConnectionResult.SUCCESS) { 
 
      goahead = false; 
 
      if (apiAvailability.isUserResolvableError(resultCode)) { 
 
       dialog = apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST); 
 
       dialog.setCancelable(false); 
 
       dialog.setCanceledOnTouchOutside(false); 
 
       dialog.show(); 
 
      } else { 
 
       Log.i("Demo App", "This device is not supported."); 
 

 
       AlertDialog.Builder dialog_app = new AlertDialog.Builder(SplashActivity.this); 
 
       dialog_app.setTitle("Error"); 
 
       dialog_app.setMessage("This device is not supported.."); 
 
       dialog_app.setCancelable(false); 
 
       dialog_app.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
 
        @Override 
 
        public void onClick(DialogInterface dialogInterface, int i) { 
 
         finish(); 
 
        } 
 
       }); 
 
       dialog_app.show(); 
 
      } 
 
      return false; 
 
     } 
 
     return true; 
 
    }

Google Playサービスが古い場合、このメソッドは更新ダイアログを表示します。そうでない場合は、trueが返され、サービスを呼び出すことができます。

チェックプレイサービスのバージョン

if(checkPlayServices()) 
 
{ 
 
     Intent intent = new Intent(this, RegistrationIntentService.class); 
 
     startService(intent); 
 
}

CustomFirebaseInstanceIDService.class:Refeshトークン

public class CustomFirebaseInstanceIDService extends FirebaseInstanceIdService { 
 
    private static final String TAG = CustomFirebaseInstanceIDService.class.getSimpleName(); 
 

 
    @Override 
 
    public void onCreate() { 
 
     super.onCreate(); 
 
    } 
 

 
    @Override 
 
    public void onTokenRefresh() { 
 
     Intent intent = new Intent(this, RegistrationIntentService.class); 
 
     startService(intent); 
 
    } 
 
}
のためのサービスクラス

フェッチFCMトークンのサービスクラス:RegistrationIntentService。クラス

public class RegistrationIntentService extends IntentService { 
 

 
    // abbreviated tag name 
 
    private static final String TAG = RegistrationIntentService.class.getSimpleName(); 
 
    public RegistrationIntentService() { 
 
     super(TAG); 
 
    } 
 
    String token; 
 
    @Override 
 
    protected void onHandleIntent(Intent intent) { 
 

 
     // Make a call to Instance API 
 

 
     FirebaseInstanceId instanceID = FirebaseInstanceId.getInstance(); 
 

 
     
 
     //String senderId = getResources().getString(R.string.gcm_defaultSenderId); 
 

 
     // request token that will be used by the server to send push notifications 
 
     token = instanceID.getToken(); 
 
     Log.d(TAG, "FCM Registration Token: " + token); 
 

 
    } 
 
}

<service android:name=".notification.CustomFirebaseInstanceIDService"> 
 
      <intent-filter> 
 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
 
      </intent-filter> 
 
     </service> 
 

 
<service 
 
      android:name=".notification.RegistrationIntentService" 
 
      android:exported="false" />
私がやった

関連する問題