Last active
October 3, 2019 06:07
-
-
Save mohitmanuja/9ebd0b8e0635ef8ad8dbb8faa36c4b9a to your computer and use it in GitHub Desktop.
Notification in Android From Firebase
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 ActivitySwitchHelper { | |
public static void rateApp() { | |
try { | |
final Intent rateIntent = new Intent(Intent.ACTION_VIEW); | |
rateIntent.setData(Uri.parse("market://details?id=" + context.getPackageName())); | |
context.startActivity(rateIntent); | |
} catch (Exception e) { | |
showToast("You don't have play store :) Thanks. "); | |
} | |
} | |
public static void promoApp(String id, String message) { | |
if (isAppInstalled("com.android.vending")) { | |
final Intent rateintent = new Intent(Intent.ACTION_VIEW); | |
rateintent.setData(Uri.parse("market://details?id=" + id)); | |
ActivitySwitchHelper.context.startActivity(rateintent); // you can pass context in function | |
if (CommonUtils.isNotNull(message)) { | |
ActivitySwitchHelper.showToast(message); | |
} | |
} else { | |
Intent i = new Intent(ActivitySwitchHelper.context, HomeScreenActivity.class); | |
ActivitySwitchHelper.context.startActivity(i); | |
} | |
} | |
} |
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
<service | |
android:name=".service.OurFirebaseMessagingService" | |
android:exported="false"> | |
<intent-filter> | |
<action android:name="com.google.firebase.MESSAGING_EVENT" /> | |
</intent-filter> | |
</service> |
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
import android.app.Activity | |
import android.content.Intent | |
import android.os.Bundle | |
import com.crashlytics.android.Crashlytics | |
class DeeplinkActivity : Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(null) | |
Crashlytics.log("onCreate ->" + this.javaClass.simpleName) | |
val intent = intent | |
val bundle = intent.extras | |
val data = intent.dataString | |
val newIntent = Intent(this, HomeScreenActivity::class.java) | |
var url: String? = intent.data?.toString() | |
if (url.isNullOrEmpty()) url = bundle?.getString("deep_link") | |
FabricEvents.trackDeeplink("Deeplink Url",url) | |
val context = ActivitySwitchHelper.getContext()// don't use this context for any service | |
// it is just to verify whether it is null or not | |
if (context != null && context is HomeScreenActivity) { | |
newIntent.action = Intent.ACTION_MAIN | |
newIntent.addCategory(Intent.CATEGORY_LAUNCHER) | |
newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP | |
newIntent.putExtra("deep_link", url) | |
startActivity(newIntent) | |
} else { | |
newIntent.action = Intent.ACTION_MAIN | |
newIntent.addCategory(Intent.CATEGORY_LAUNCHER) | |
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP | |
newIntent.putExtra("deep_link", url) | |
startActivity(newIntent) | |
} | |
finish() | |
} | |
override fun onResume() { | |
super.onResume() | |
ActivitySwitchHelper.setContext(this) | |
} | |
override fun onStop() { | |
super.onStop() | |
Crashlytics.log("onStop ->" + javaClass.simpleName) | |
} | |
} |
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
import android.net.Uri | |
object DeepLinkHandleProvider { | |
init { | |
} | |
fun openThisUrl(url: String?) { | |
if (CommonUtils.isNotNull(url)){ | |
parseUrl(url!!) | |
} | |
} | |
private fun parseUrl(url: String) { | |
var splitResult: Array<String>? | |
val urlToSplit = url.replace("http://", "").replace("https://", "") | |
splitResult = urlToSplit.split("[/]".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
var lastPart: String? = null | |
var pageType: String? = null | |
if (splitResult.size > 1) { | |
pageType = splitResult[1] | |
lastPart = splitResult[splitResult.size - 1] | |
} else if (splitResult.size == 1) { | |
// return; | |
} | |
var eid: String? = null | |
if (lastPart != null) { | |
val array = lastPart.split("[#!?]".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | |
if (array.isNotEmpty()) { | |
eid = array[0] | |
} | |
} | |
openNotificationPage(pageType, eid, url) | |
} | |
private fun openNotificationPage(pageType: String?, eid: String?, url: String) { | |
val uri = Uri.parse(url) | |
val inWebview = uri.getBooleanQueryParameter("in_web_view", false) | |
val inChrome = uri.getBooleanQueryParameter("in_chrome", false) | |
val packageId = uri.getQueryParameter("packageId") | |
val name = uri.getQueryParameter("name") | |
if (inWebview){ | |
ActivitySwitchHelper.openInAppBrowser(url, name, true) | |
return | |
} | |
if (inChrome){ | |
ActivitySwitchHelper.openInBrowser(url) | |
return | |
} | |
if("promo" == pageType){ | |
ActivitySwitchHelper.promoApp(eid, "") | |
return | |
} | |
if ("rateus" == pageType) { | |
ActivitySwitchHelper.rateApp() | |
return | |
} | |
ActivitySwitchHelper.openInBrowser(url) | |
} | |
} | |
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
Call openIntent in HomeScreenActivity in onCreate() | |
public void openIntent(Intent intent) { | |
if (intent == null) { | |
return; | |
} | |
Bundle extras = intent.getExtras(); | |
if (extras == null) { | |
return; | |
} | |
if (extras.containsKey("deep_link")) { | |
DeepLinkHandleProvider.INSTANCE.openThisUrl(extras.getString("deep_link")); | |
} | |
} |
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
import android.app.Notification | |
import android.app.NotificationManager | |
import android.app.PendingIntent | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Build | |
import android.os.Bundle | |
import android.util.Log | |
import com.google.firebase.messaging.FirebaseMessagingService | |
import com.google.firebase.messaging.RemoteMessage | |
class OurFirebaseMessagingService : FirebaseMessagingService() { | |
override fun onMessageReceived(remoteMessage: RemoteMessage?) { | |
super.onMessageReceived(remoteMessage) | |
if(remoteMessage!=null){ | |
val data = remoteMessage.data | |
val extras = getBundleFromMap(data) | |
if (!extras.isEmpty) { | |
buildNotification(extras) | |
} | |
} | |
} | |
override fun onNewToken(p0: String?) { | |
super.onNewToken(p0) | |
Log.d("token", p0) | |
} | |
private fun getBundleFromMap(map: Map<String, String>?): Bundle { | |
val bundle = Bundle() | |
if (map != null) { | |
for (key in map.keys) { | |
val value = map[key] | |
bundle.putString(key, value) | |
} | |
} | |
return bundle | |
} | |
private fun buildNotification(messageObject: Bundle) { | |
val title = messageObject.getString("title", "") | |
val text = messageObject.getString("text", "") | |
val url = messageObject.getString("deep_link", "") | |
val routeIntent = Intent(this, DeeplinkActivity::class.java) | |
routeIntent.putExtra("deep_link", url) | |
val contentIntent = PendingIntent.getActivity(this, 0, | |
routeIntent, PendingIntent.FLAG_UPDATE_CURRENT) | |
val mBuilder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
Notification.Builder(this, CustomNotificationChannels.DEFAULT) | |
.setContentTitle(title) | |
.setPriority(android.app.Notification.PRIORITY_MAX) | |
.setContentText(text) | |
.setSmallIcon(R.drawable.icon) | |
.setAutoCancel(true) | |
.setContentIntent(contentIntent) | |
} else { | |
Notification.Builder(this) | |
.setContentTitle(title) | |
.setPriority(android.app.Notification.PRIORITY_MAX) | |
.setContentText(text) | |
.setSmallIcon(R.drawable.icon) | |
.setAutoCancel(true) | |
.setContentIntent(contentIntent) | |
} | |
val mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
mNotificationManager.notify(1, mBuilder.build()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment