Last active
May 20, 2020 07:24
-
-
Save yunusemredilber/97ed50e7ca302611c6801871e0a0ad85 to your computer and use it in GitHub Desktop.
Android Firebase Cloud Messaging Skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
package="com.example.example"> | |
<!-- ... --> | |
<application | |
<!-- ... --> > | |
<!-- ... --> | |
<!-- Firebase start --> | |
<service | |
android:name=".MyFirebaseMessagingService" | |
android:exported="false"> | |
<intent-filter> | |
<action android:name="com.google.firebase.MESSAGING_EVENT" /> | |
</intent-filter> | |
</service> | |
<!-- If you want to get data when app is not running --> | |
<receiver | |
android:name=".FirebaseDataReceiver" | |
android:exported="true" | |
android:permission="com.google.android.c2dm.permission.SEND"> | |
<intent-filter> | |
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> | |
</intent-filter> | |
</receiver> | |
<meta-data | |
android:name="com.google.firebase.messaging.default_notification_channel_id" | |
android:value="123" /> | |
<meta-data | |
android:name="firebase_messaging_auto_init_enabled" | |
android:value="false" /> | |
<meta-data | |
android:name="firebase_analytics_collection_enabled" | |
android:value="false" /> | |
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. | |
See README(https://goo.gl/l4GJaQ) for more. --> | |
<meta-data | |
android:name="com.google.firebase.messaging.default_notification_icon" | |
android:resource="@drawable/ic_arrow_left" /> | |
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming | |
notification message. See README(https://goo.gl/6BKBk7) for more. --> | |
<meta-data | |
android:name="com.google.firebase.messaging.default_notification_color" | |
android:resource="@color/colorAccent" /> | |
<!-- Firebase end --> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Add the following code where you want to initialize fcm. | |
FirebaseMessaging.getInstance().setAutoInitEnabled(true); | |
FcmHelper.saveFCMToken(); | |
**/ | |
public class FcmHelper { | |
public static void saveFCMToken() { | |
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> { | |
if (!task.isSuccessful()) { | |
Log.d("DEBUG", "FCM getInstanceId failed", task.getException()); | |
return; | |
} | |
// Get new Instance ID token | |
String token = Objects.requireNonNull(task.getResult()).getToken(); | |
// TODO: Start token saving and sending scenario | |
Log.d("DEBUG", "FCM getInstanceId new token -> " + token); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirebaseDataReceiver extends BroadcastReceiver { | |
Context mContext; | |
@Override | |
public void onReceive(Context context, Intent it) { | |
Log.w("DEBUG", "FirebaseDataReceiver onReceive"); | |
this.mContext = context; | |
Bundle bundle = it.getExtras(); | |
if(bundle == null) { | |
Log.w("DEBUG", "FirebaseDataReceiver bundle is null"); | |
return; | |
} | |
if(isAppOnForeground(context)) { | |
Log.w("DEBUG", "FirebaseDataReceiver app is running"); | |
// Don't do anything because MyFirebaseMessagingService will receive the message. | |
return; | |
} | |
// TODO: App in background. Do something. | |
// No need to display a notification manually. Firebase will show a notification. | |
} | |
private boolean isAppOnForeground(Context context) { | |
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); | |
if (appProcesses == null) { | |
return false; | |
} | |
final String packageName = context.getPackageName(); | |
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { | |
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
/** | |
* Called if InstanceID token is updated. This may occur if the security of | |
* the previous token had been compromised. Note that this is called when the InstanceID token | |
* is initially generated so this is where you would retrieve the token. | |
*/ | |
@Override | |
public void onNewToken(@NonNull String token) { | |
Log.i("DEBUG", "MyFirebaseMessagingService onNewToken: " + token); | |
// TODO: Start token saving and sending scenario | |
} | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
Log.i("DEBUG", "MyFirebaseMessagingService onMessageReceived: " + remoteMessage.toString()); | |
Log.i("DEBUG", "MyFirebaseMessagingService From: " + remoteMessage.getFrom()); | |
Log.i("DEBUG", "MyFirebaseMessagingService data payload: " + remoteMessage.getData()); | |
// TODO: App in foreground. Do something. | |
// Map<String, String> data = remoteMessage.getData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment