CopyPastor

Detecting plagiarism made easy.

Score: 1.8707995414733887; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2021-11-28
by Abhishek Chhabra

Original Post

Original - Posted on 2017-01-06
by Eliasz Kubala



            
Present in both answers; Present only in the new answer; Present only in the old answer;

FASTEST AND GOOD FOR PROTOTYPE
The quick solution is to store it in shared prefs and add this logic to the onCreate method in your MainActivity or class which is extending Application.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, instanceIdResult -> { String newToken = instanceIdResult.getToken(); Log.e("newToken", newToken); getActivity().getPreferences(Context.MODE_PRIVATE).edit().putString("fb", newToken).apply(); }); Log.d("newToken", getActivity().getPreferences(Context.MODE_PRIVATE).getString("fb", "empty :("));
CLEANER WAY A better option is to create a service and keep inside a similar logic. Firstly create new Service
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String s) { super.onNewToken(s); Log.e("newToken", s); getSharedPreferences("_", MODE_PRIVATE).edit().putString("fb", s).apply(); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); } public static String getToken(Context context) { return context.getSharedPreferences("_", MODE_PRIVATE).getString("fb", "empty"); } }
And then add it to AndroidManifest file
<service android:name=".MyFirebaseMessagingService" android:stopWithTask="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
Finally, you are able to use a static method from your Service `MyFirebaseMessagingService.getToken(Context);`
THE FASTEST BUT DEPRECATED
Log.d("Firebase", "token "+ FirebaseInstanceId.getInstance().getToken());
It's still working when you are using older firebase library than version 17.x.x


### FASTEST AND GOOD FOR PROTOTYPE
The quick solution is to store it in sharedPrefs and add this logic to `onCreate` method in your MainActivity or class which is extending Application.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, instanceIdResult -> { String newToken = instanceIdResult.getToken(); Log.e("newToken", newToken); getActivity().getPreferences(Context.MODE_PRIVATE).edit().putString("fb", newToken).apply(); });
Log.d("newToken", getActivity().getPreferences(Context.MODE_PRIVATE).getString("fb", "empty :("));
### CLEANER WAY
A better option is to create a service and keep inside a similar logic. Firstly create new Service

public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String s) { super.onNewToken(s); Log.e("newToken", s); getSharedPreferences("_", MODE_PRIVATE).edit().putString("fb", s).apply(); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); } public static String getToken(Context context) { return context.getSharedPreferences("_", MODE_PRIVATE).getString("fb", "empty"); } }
And then add it to AndroidManifest file
<service android:name=".MyFirebaseMessagingService" android:stopWithTask="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
Finally, you are able to use a static method from your Service `MyFirebaseMessagingService.getToken(Context);`
### THE FASTEST BUT DEPRECATED
Log.d("Firebase", "token "+ FirebaseInstanceId.getInstance().getToken());
It's still working when you are using older firebase library than version 17.x.x

        
Present in both answers; Present only in the new answer; Present only in the old answer;