DwayneCoussement / flutter_geofence

BSD 3-Clause "New" or "Revised" License
42 stars 40 forks source link

not executed when terminated #14

Open niypoo opened 4 years ago

niypoo commented 4 years ago

I have working on ios and it's working great but when test it in android , it only working when is opening but when I close the app , it doesn't listen any events, I have tested on emu android 10, and physical mobile Samsung android 9.

and I'm using android with embedding v2.

niypoo commented 4 years ago

2020-04-16 13:36:52.201 3718-12657/? W/Bundle: Attempt to cast generated internal exception: java.lang.ClassCastException: java.lang.Integer cannot be cast to android.os.Parcelable at android.os.Bundle.getParcelable(Bundle.java:946) at android.app.Notification.fixDuplicateExtra(Notification.java:2841) at android.app.Notification.fixDuplicateExtras(Notification.java:2831) at android.app.Notification.readFromParcelImpl(Notification.java:2179) at android.app.Notification.(Notification.java:2107) at android.service.notification.StatusBarNotification.(StatusBarNotification.java:104) at android.service.notification.StatusBarNotification$1.createFromParcel(StatusBarNotification.java:194) at android.service.notification.StatusBarNotification$1.createFromParcel(StatusBarNotification.java:191) at android.os.Parcel.readParcelable(Parcel.java:2790) at android.os.Message.readFromParcel(Message.java:622) at android.os.Message.access$000(Message.java:34) at android.os.Message$1.createFromParcel(Message.java:578) at android.os.Message$1.createFromParcel(Message.java:575) at android.os.IMessenger$Stub.onTransact(IMessenger.java:52) at android.os.Binder.execTransact(Binder.java:739)

Dharm23 commented 3 years ago

Hello @niypoo , because in android the dart not able to execute code in background so you need to modified GeofenceBroadcastReceiver.kt from you android studio, as follow:

override fun onReceive(context: Context, intent: Intent) { Log.d("DC", "Called onreceive") val geofencingEvent = GeofencingEvent.fromIntent(intent) if (geofencingEvent.hasError()) { Log.e(TAG, "something went wrong") return }

    // Get the transition type.
    val geofenceTransition = geofencingEvent.geofenceTransition

    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
        val event = if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) GeoEvent.entry else GeoEvent.exit
        val triggeringGeofences = geofencingEvent.triggeringGeofences

        for (geofence: Geofence in triggeringGeofences) {
            val region = GeoRegion(id=geofence.requestId,
                    latitude = geofencingEvent.triggeringLocation.latitude,
                    longitude = geofencingEvent.triggeringLocation.longitude,
                    radius = 50.0.toFloat(),
                    events = listOf(event)
                    )
             //custom change to get notification even app is terminate 
            **if(event == GeoEvent.entry){
                sendNotification("Infinite Automation", strDetails = "Welcome to " + region.id, context = context);
            }else{
                sendNotification("Infinite Automation", strDetails = "Exit to " + region.id, context = context);
            }**
            callback?.invoke(region)
            Log.i(TAG, region.toString())
        }
    } else {
        // Log the error.
        Log.e(TAG, "Not an event of interest")
    }
} 

private fun sendNotification(strTital: String, strDetails: String, context: Context){
    try{
        val mNotificationManager: NotificationManager

        val mBuilder = NotificationCompat.Builder(context.getApplicationContext(), "notify_001")

        val bigText = NotificationCompat.BigTextStyle()
        bigText.bigText(strTital)
        bigText.setBigContentTitle(strDetails)
        bigText.setSummaryText(strDetails)

        mBuilder.setSmallIcon(R.drawable.notification_icon)
        mBuilder.setContentTitle(strTital)
        mBuilder.setContentText(strDetails)
        mBuilder.priority = Notification.PRIORITY_MAX
        mBuilder.setStyle(bigText)

        mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "Your_channel_id"
            val channel = NotificationChannel(
                    channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH)
            mNotificationManager.createNotificationChannel(channel)
            mBuilder.setChannelId(channelId)
        }
        mNotificationManager.notify(0, mBuilder.build())
        Log.i(TAG, "Notification send complete")
    }catch (ex:Exception){
        Log.i(TAG, ex.toString())
    }

}