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);
}
}
};
を呼び出しています、通知もありませんデータベースには挿入されません。
私にこれを手伝ってもらえますか?
は私が意図 – asifa
を作成したYのthatsを働いていないのは、あなたの携帯デビックであることのマニフェストファイルでサービスを定義しますGoogle Playサービスを利用していますか? –
はいGoogleのプレイサービスアプリ – asifa