Reputation: 11025
For testing FCM, it only needs to create a FirebaseMessagingService and run the app, the token will be passed to it through its onNewToken(String token). (sure need to create a project with FCM console, and download the google-service.json).
The question is how does the FCM know there is an app just start to run and FCM should send the token to it (how)?
the test project has only a mainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
and a service:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@SuppressLint("LongLogTag")
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}
@SuppressLint("LongLogTag")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
//...
}
}
plus added the google-services.json got from the FCM project console. and in respective build.gradle added the apply plugin, following the google FCM site instruction:
apply plugin: 'com.google.gms.google-services'
dependencies {
classpath 'com.google.gms:google-services:4.3.3'
and in manifect:
<service android:name="com.demo.mytestfcmnotificationdemo.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Upvotes: 0
Views: 58
Reputation: 598765
It actually works the other way around: when your app starts, it calls the FCM services and asks them it the token it has is still valid. If it is no longer valid, it gets a new token and then calls this onNewToken
handler.
So FCM is not actively scanning for tokens that need updating, but waits for the device to reach out and check the status of its token.
Upvotes: 1