Created
December 25, 2023 11:52
-
-
Save kishan-vadoliya/72af4f1aa92fc7341fe0ab83d34fc21f to your computer and use it in GitHub Desktop.
OneSignalSetup
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
OneSignalUtils.setupOneSignal(instance.applicationContext) | |
// Resource Required | |
1. small icon | |
2. large icon |
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
import android.content.Context | |
import android.graphics.BitmapFactory | |
import com.onesignal.OSMutableNotification | |
import com.onesignal.OSNotificationReceivedEvent | |
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler | |
@SuppressWarnings("unused") | |
class NotificationServiceExtension : OSRemoteNotificationReceivedHandler { | |
private var storeUserData: StoreUserData? = null | |
override fun remoteNotificationReceived( | |
context: Context?, | |
notificationReceivedEvent: OSNotificationReceivedEvent? | |
) { | |
try { | |
if (notificationReceivedEvent != null && context != null) { | |
val notification = notificationReceivedEvent.notification | |
if (notification != null) { | |
val mutableNotification: OSMutableNotification = notification.mutableCopy() | |
mutableNotification.setExtender { builder -> | |
builder.priority = notification.priority | |
builder.setLargeIcon( | |
BitmapFactory.decodeResource( | |
context.resources, | |
R.mipmap.ic_launcher | |
) | |
) | |
builder.setSmallIcon(R.drawable.ic_noti_small) | |
} | |
notificationReceivedEvent.complete(mutableNotification) | |
} else { | |
notificationReceivedEvent.complete(null) | |
} | |
} | |
} catch (e: NullPointerException) { | |
e.printStackTrace() | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
} |
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
import com.onesignal.OSNotificationReceivedEvent | |
import com.onesignal.OneSignal | |
class OneSignalNotificationForegroundHandler(private val application: MyApplication) : | |
OneSignal.OSNotificationWillShowInForegroundHandler { | |
override fun notificationWillShowInForeground(notificationReceivedEvent: OSNotificationReceivedEvent?) { | |
try { | |
if (notificationReceivedEvent != null) { | |
if (notificationReceivedEvent.notification != null) { | |
val notification = notificationReceivedEvent.notification | |
val additionalData = notification.additionalData | |
if (additionalData != null) { | |
notificationReceivedEvent.complete(notification) | |
} else { | |
notificationReceivedEvent.complete(null) | |
} | |
} else { | |
notificationReceivedEvent.complete(null) | |
} | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
} |
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
import android.content.Intent | |
import androidx.core.content.ContextCompat | |
import com.onesignal.OSNotificationOpenedResult | |
import com.onesignal.OneSignal | |
class OneSignalNotificationOpenHandler(private val application: MyApplication) : | |
OneSignal.OSNotificationOpenedHandler { | |
override fun notificationOpened(result: OSNotificationOpenedResult?) { | |
try { | |
if (result != null) { | |
val type = if (result.notification.additionalData != null) { | |
if (result.notification.additionalData.has(NOTIFICATION_TYPE)) | |
result.notification.additionalData.getString(NOTIFICATION_TYPE) | |
else "" | |
} else { | |
"" | |
} | |
when (type) { | |
NOTIFICATION_TYPE_CONTENTS -> { | |
/* getContent( | |
result.notification.additionalData.getString(NOTIFICATION_CONTENT_ID), | |
type | |
) | |
*/ | |
} | |
NOTIFICATION_CATEGORY -> { | |
openActivity( | |
type, | |
null, | |
result.notification.additionalData.getString(NOTIFICATION_CONTENT_ID) | |
) | |
} | |
NOTIFICATION_GRAPHICS -> { | |
openActivity( | |
type, | |
null, | |
result.notification.additionalData.getString(NOTIFICATION_CONTENT_ID) | |
) | |
} | |
NOTIFICATION_TYPE_SEARCH -> { | |
openActivity( | |
type, | |
null, | |
result.notification.additionalData.getString(NOTIFICATION_TAG) | |
) | |
} | |
/* NOTIFICATION_TYPE_CONTENT_TAGS -> { | |
openActivity( | |
type, | |
null, | |
result.notification.additionalData.getString(NOTIFICATION_CONTENT_ID) | |
) | |
}*/ | |
else -> { | |
openActivity(type, null) | |
} | |
} | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
private fun openActivity(type: String, dataBean: UserContentRes.Data?, tagString: String? = null) { | |
try { | |
var intentReceiver = | |
Intent( | |
application, | |
SplashActivity::class.java | |
) | |
intentReceiver.putExtra(Constant.KEY_SCREEN, type) | |
if (dataBean != null) { | |
intentReceiver.putExtra(Constant.KEY_DATA_BEAN, dataBean) | |
} | |
if (tagString != null) { | |
if (tagString.isNotEmpty()) | |
intentReceiver.putExtra(NOTIFICATION_TAG, tagString) | |
} | |
intentReceiver.flags = | |
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK | |
intentReceiver.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY) | |
ContextCompat.startActivity( | |
application, | |
intentReceiver, | |
null | |
) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
} |
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
import android.content.Context | |
import android.os.Build | |
import com.onesignal.OneSignal | |
object OneSignalUtils { | |
fun setupOneSignal(context: Context) { | |
try { | |
val storeUserData = StoreUserData(context) | |
// Logging set to help debug issues, remove before releasing your app. | |
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE) | |
OneSignal.initWithContext(MyApplication.instance) | |
OneSignal.setAppId(context.getString(R.string.onesignal_app_id)) | |
OneSignal.setLocationShared(false) | |
OneSignal.setNotificationOpenedHandler(OneSignalNotificationOpenHandler(MyApplication.instance)) | |
OneSignal.setNotificationWillShowInForegroundHandler( | |
OneSignalNotificationForegroundHandler(MyApplication.instance) | |
) | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | |
OneSignal.disablePush(!MyApplication.instance.storeUserData.getBoolean(Constant.IS_NOTIFICATION_ENABLED)) | |
} | |
OneSignal.unsubscribeWhenNotificationsAreDisabled(false) | |
OneSignal.addPermissionObserver {} | |
OneSignal.addSubscriptionObserver { | |
if (it != null) { | |
if (it.to.isSubscribed) { | |
storeUserData.setString( | |
Constant.ANDROID_ONESIGNAL_PLAYERID, it.to.userId | |
) | |
} | |
} | |
} | |
if (OneSignal.getDeviceState() != null) { | |
if (OneSignal.getDeviceState()!!.pushToken != null) { | |
storeUserData.setString( | |
Constant.ANDROID_FCM_TOKEN, OneSignal.getDeviceState()!!.pushToken | |
) | |
} | |
if (OneSignal.getDeviceState()!!.userId != null) { | |
storeUserData.setString( | |
Constant.ANDROID_ONESIGNAL_PLAYERID, OneSignal.getDeviceState()!!.userId | |
) | |
} | |
} else { | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} | |
fun getOneSignalPlayerId(context: Context): String { | |
try { | |
val storeUserData = StoreUserData(context) | |
if (storeUserData.getString(Constant.ANDROID_ONESIGNAL_PLAYERID)!!.isEmpty()) { | |
if (OneSignal.getDeviceState() != null) { | |
if (OneSignal.getDeviceState()!!.userId != null && OneSignal.getDeviceState()!!.userId.isNotEmpty()) { | |
storeUserData.setString( | |
Constant.ANDROID_ONESIGNAL_PLAYERID, OneSignal.getDeviceState()!!.userId | |
) | |
return storeUserData.getString(Constant.ANDROID_ONESIGNAL_PLAYERID)!! | |
} | |
} | |
} else { | |
return storeUserData.getString(Constant.ANDROID_ONESIGNAL_PLAYERID)!! | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
return "" | |
} | |
} |
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
// Project level gradle | |
id("com.onesignal.androidsdk.onesignal-gradle-plugin") version("0.14.0") apply(false) | |
// App level gradle | |
implementation("com.onesignal:OneSignal:4.8.5") | |
// Manifest.kt | |
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- Notification Permission --> | |
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
<receiver | |
android:name=".notifyme.NotificationPublisher" | |
android:enabled="true" | |
android:exported="true" | |
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> | |
<intent-filter> | |
<action android:name="android.intent.action.QUICKBOOT_POWERON" /> | |
<action android:name="android.intent.action.BOOT_COMPLETED" /> | |
<category android:name="android.intent.category.HOME" /> | |
</intent-filter> | |
</receiver> | |
<meta-data | |
android:name="com.onesignal.NotificationOpened.DEFAULT" | |
android:value="DISABLE" /> | |
<meta-data | |
android:name="com.onesignal.NotificationServiceExtension" | |
android:value="com.video.quotes.creator.maker.vidqo.app.notification.NotificationServiceExtension" /> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment