Last active
October 22, 2020 08:43
-
-
Save abhishekgargx/e8c5b48cfc621f9c53edbb4a12e452db to your computer and use it in GitHub Desktop.
Android Push Notification using Firebase Cloud Messaging ( FCM ) , works in Foreground , background , app killed, dead. includes Notification Actions - Abhishek Garg
This file contains hidden or 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
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.example"> | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <application | |
| android:name=".MainApplication" | |
| android:label="@string/app_name" | |
| android:icon="@mipmap/ic_launcher" | |
| android:allowBackup="false" | |
| android:theme="@style/AppTheme"> | |
| <activity | |
| android:name=".MainActivity" | |
| android:label="@string/app_name" | |
| android:configChanges="keyboard|keyboardHidden|orientation|screenSize" | |
| android:windowSoftInputMode="adjustResize"> | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| <!--SERVICE FOR FIREBASE NOTIFICATION--> | |
| <service android:name=".NotificationService" android:stopWithTask="false"> | |
| <intent-filter> | |
| <action android:name="com.google.firebase.MESSAGING_EVENT"/> | |
| </intent-filter> | |
| </service> | |
| </application> | |
| </manifest> |
This file contains hidden or 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
| package package_name; | |
| import android.app.Notification; | |
| import android.media.RingtoneManager; | |
| import android.net.Uri; | |
| import android.support.v4.app.NotificationCompat; | |
| import android.support.v4.app.NotificationManagerCompat; | |
| import android.util.Log; | |
| import com.google.firebase.messaging.FirebaseMessagingService; | |
| import com.google.firebase.messaging.RemoteMessage; | |
| /** | |
| * | |
| * Author : Abhishek Garg | |
| * | |
| */ | |
| /** | |
| * This is notification service for whole app, it handles notification from Firebase (FCM) | |
| * (whether you send it from backend or directly firebase) | |
| * | |
| */ | |
| public class NotificationService extends FirebaseMessagingService { | |
| @Override | |
| public void onMessageReceived(RemoteMessage remoteMessage) { | |
| super.onMessageReceived(remoteMessage); | |
| //notification title | |
| String title = ""; | |
| //notification body | |
| String body = ""; | |
| // notification custom data (i am getting activity name to open based on custom data i send) | |
| String activity_name = ""; | |
| // HANDLING DATA FROM REMOTE MESSAGE FOR FIREBASE AND BACKEND | |
| // if notification object is like this ( Our Backend send this way and firebase send this way when app is background) | |
| // { | |
| // "to": "the device token" | |
| // "data":{ | |
| // "title":"New Notification!", | |
| // "body":"Test" | |
| // }, | |
| // "priority":10 | |
| // } | |
| if(remoteMessage.getData() != null){ | |
| JSONObject json = new JSONObject(remoteMessage.getData()); | |
| Iterator itr = json.keys(); | |
| while (itr.hasNext()) { | |
| String key = (String) itr.next(); | |
| switch (key){ | |
| case "title" : { | |
| try { | |
| title = json.getString(key); | |
| } catch (JSONException e) { | |
| // Utility.showToast(this,"json exception in notification in title" ); | |
| } | |
| break; | |
| } | |
| case "body" : { | |
| try { | |
| body = json.getString(key); | |
| } catch (JSONException e) { | |
| // Utility.showToast(this,"json exception in notification in body" ); | |
| } | |
| break; | |
| } | |
| case "activity_name" : { | |
| try { | |
| activity_name = json.getString(key); | |
| } catch (JSONException e) { | |
| // Utility.showToast(this,"json exception in notification in activity name" ); | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| // if notification object is like this (By Default Firebase (UI Version) send this way) | |
| // { | |
| // "to": "the device token" | |
| // "notification":{ | |
| // "title":"New Notification!", | |
| // "body":"Test" | |
| // }, | |
| // "priority":10 | |
| // } | |
| if(remoteMessage.getNotification() != null){ | |
| title = remoteMessage.getNotification().getTitle(); | |
| body = remoteMessage.getNotification().getBody(); | |
| } | |
| // notification icon background color | |
| int notificationColor = getResources().getColor(R.color.color_orange); | |
| // notification icon | |
| int notificationIcon = getNotificationIcon(); | |
| // notification sound | |
| Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
| // notification channel id | |
| String channelId = "default" | |
| // notification id | |
| int notification_id = 123; | |
| //create notification for Oreo devices | |
| createNotificationChannel(); | |
| // notification Action | |
| // open activity you want on press of notification | |
| Intent intent; | |
| PendingIntent pendingIntent = null; | |
| if (acitivity_name != null) { | |
| switch (activity_name){ | |
| case "open_pp_acitivity": { | |
| intent = new Intent(this, PPActivity.class); | |
| break; | |
| } | |
| case "open_google_com": { | |
| intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com")); | |
| break; | |
| } | |
| default:{ | |
| intent = new Intent(this, MainActivity.class); | |
| } | |
| } | |
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
| pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT); | |
| }else{ | |
| intent = new Intent(this, MainActivity.class); | |
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
| pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT); | |
| } | |
| Notification notification = new NotificationCompat.Builder(this,channelId) | |
| .setContentTitle(title) | |
| .setContentText(body) | |
| .setSmallIcon(notificationIcon) | |
| .setColor(notificationColor) | |
| .setSound(notificationSound) | |
| .setStyle(new NotificationCompat.BigTextStyle().bigText(body)) | |
| .setPriority(NotificationCompat.PRIORITY_MAX) | |
| .setContentIntent(pendingIntent) | |
| .build(); | |
| NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext()); | |
| // | |
| manager.notify(notification_id, notification); | |
| } | |
| @TargetApi(Build.VERSION_CODES.O) | |
| private void createNotificationChannel() { | |
| String channelId = getString(R.string.default_notification_channel_id); | |
| // Create the NotificationChannel, but only on API 26+ because | |
| // the NotificationChannel class is new and not in the support library | |
| if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| CharSequence name = "channel_name"; | |
| String description = "channel_discription"; | |
| int importance = NotificationManager.IMPORTANCE_HIGH; | |
| NotificationChannel channel = new NotificationChannel(channelId, name, importance); | |
| channel.setDescription(description); | |
| // Register the channel with the system; you can't change the importance | |
| // or other notification behaviors after this | |
| NotificationManager notificationManager = getSystemService(NotificationManager.class); | |
| notificationManager.createNotificationChannel(channel); | |
| } | |
| } | |
| // NOTIFICATION ICON ACCORDING TO CURRENT ANDROID VERSION | |
| private int getNotificationIcon() { | |
| boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP); | |
| // ic_stat_logo_black is logo generated for notification using tool : http://romannurik.github.io/AndroidAssetStudio/ | |
| return useWhiteIcon ? R.drawable.ic_stat_logo_black : R.mipmap.ic_launcher; | |
| } | |
| } |
This file contains hidden or 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 SplashActivity extends AppCompatActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_splash); | |
| // when app is in background or swiped from recent , handle custom data or intents or actions like below | |
| // below code should always in your app launcher activity , onCreate method , in my case it is SplashActivity | |
| if (getIntent().getExtras() != null) { | |
| Bundle bundle = getIntent().getExtras(); | |
| if(bundle != null) { | |
| // activity_name is key name of custom data i sending | |
| String value = bundle.getString("activity_name"); | |
| if( value != null){ | |
| // perform any condition you want to execute based on value | |
| // i am starting google.com in web browser | |
| switch(value){ | |
| case "google":{ | |
| startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.com"))); | |
| break; | |
| } | |
| default:{ | |
| startActivity(new Intent(this, MainActivity.class) ); | |
| break; | |
| } | |
| } | |
| }else{ | |
| startActivity(new Intent(this, MainActivity.class) ); | |
| } | |
| } | |
| else{ | |
| startActivity(new Intent(this, MainActivity.class) ); | |
| } | |
| } | |
| else{ | |
| startActivity(new Intent(this, MainActivity.class) ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment