Android notifications

4

I have the following problem. I have an app that receives push notifications from firebase. So far excellent.

When I receive the notification, what I do is create a notification in the following way:

private fun sendNotification(messageBody: String, title: String) {
        val intent =  Intent(this, NotificacionesActivity::class.java)
        intent.setClassName(this.packageName, "${this.packageName}.NotificacionesActivity")
        intent.putExtra("notificacion",true)
        intent.action = Intent.ACTION_VIEW
        intent.addCategory(Intent.CATEGORY_INFO)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT)

        //val channelId:Int = getString(R.string.default_notification_channel_id)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val notificationBuilder = NotificationCompat.Builder(this, ""/*, channelId*/)

                .setSmallIcon(R.drawable.ic_mail)
                .setContentTitle(""+title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
    }

Two interesting things happen here:

  • If the application is in the foreground and I open the notification from the notification manager, the activity I require is opened perfectly.

  • If the application is in the background and I open the notification from the notification manager, the Login activity that is declared as launcher in the manifest opens.

  • What I require is that the first option always happens is to say that the ActivityActivity activity is opened even if the app is running in the background.

    Is this possible? I will be forgetting some property of the notifications and for that reason I do not obtain the expected result in both scenarios?

    Thank you in advance.

    Greetings.

        
    asked by Gustavo Mora 15.02.2018 в 16:10
    source

    2 answers

    0

    The problem I managed to solve and the same is not the fact of generating a notification as such on Android.

    When a push notification is generated from the firebase console, 2 possible scenarios will occur on the device once the notification information is received.

    Scenario 1. The application is in the background: The notification is received and stored in the notification center.

    Scenario 2. The application in the foreground : if we implement, as in my case, a "service" that inherits from FirebaseMessagingService in the onMessageReceived(...) method, we will receive the push notification as the% method is executed. sendNotification .... In this case the activity NotificacionesActivity

    will open correctly

    Note the notification in firebase in the first instance I was assembling it in the following way:

    In the "message text" field I initially thought that this is always received in the application but it really is not so for scenario 1 where the application is in the background. What I mean is that the text of the message does not reach the application if it is in the background since the notification is generated automatically and stored in the notification center.

    To solve the above, adjust the advanced properties from the console so that if you add parameters this data will reach the application even if it is in the background.

    In the part marked in red does not necessarily have to go a messages or message titles, there you can travel information of any kind and you can implement any functionality on them.

    With the above, even if the app is in the background, and the notification goes directly to the Android notification center, pressing it will open the application and the parameters can be read in the following way:

    if(getIntent()!=null)
                if (getIntent().getExtras()!=null)
                      if(getIntent().getExtras().containsKey("Cuerpo_Mensaje")) {
    
                        String mensaje = getIntent().getExtras().getString("Cuerpo_Mensaje");
                        String titulo = "";
                        if (getIntent().getExtras().containsKey("Titulo_Mensaje"))
                            titulo = getIntent().getExtras().getString("Titulo_Mensaje");
    }
    

    Later in my case I decided to save the notification in a local database and show it in the notification activity.

    The intent information will arrive at the activity that is declared as launcher in the manifest, so that is where the extras have to read.

        
    answered by 14.09.2018 / 16:37
    source
    0

    Try changing

     //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT)
    

    a

    val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT)
    

    Look at the change the flag that is used.

        
    answered by 15.02.2018 в 18:05