Firebase notifications

0

I would like to know if someone could help me with a detail I have with Android. The scenario is as follows:

I have a Webview that I charge in my main activity, and all good, if I leave the application and login again it does not reload the page.

The problem is when I receive a notification with Firebase , if I open the notification send me to call again the method onCreate() of my activity and consequently recharge my WebView , situation that I want to avoid. The question is:

  

Can you open my application by clicking on the notification without   have to go through the onCreate () method?

I hope someone can guide me. Greetings.

    
asked by Jarboox 25.11.2016 в 19:49
source

3 answers

0
  

Can you open my application by clicking on the notification without   have to go through the onCreate method?

To open the application is done through a PendingIntent , here are several answers on how to do it.

link

link

link

Regarding that you want to open the application and do not want it to go through onCreate() , remember that to open an Activity you necessarily have to go through onCreate() - > onStart() - > onResume() , this is defined in the life cycle of the Activity:

Therefore it is not possible that it does not pass through onCreate() unless you previously opened the application.

    
answered by 25.11.2016 / 21:21
source
0

@Gonzalo GM, this is my code to create the notification:

private void showNotification(Map<String, String> data) {

    Intent i = new Intent(this, MainActivity.class);

    //i.putExtra("Activity", data.get("Main"));
    i.putExtra("Activity", "Main");

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);

    long[] pattern = new long[] { 2000, 500, 2000 };

    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(data.get("Title"))
            .setVibrate(pattern)
            .setSound(defaultSound)
            .setWhen(System.currentTimeMillis())
            .setContentText(data.get("Message"))
            .setSmallIcon(R.drawable.redgps)
            .setContentIntent(pendingIntent);

    int defaults = 0;

    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    builder.setDefaults(defaults);

    Random r = new Random();

    int idNotificacionG = r.nextInt(80 - 65) + 65;

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(idNotificacionG, builder.build());
}
    
answered by 25.11.2016 в 20:32
0

Jarbox using the Firebase console can not customize the click event in the notification. To do this you have to do the following:

  • You have to make a method in the firebaseService class that when you get the token, send it to a database on a dedicated server for that.

  • You have to create your own console, that is, make a small web app, where you have all the fields you want to send in your message eg title, content, etc.

  • Once you have your html, when you submit a button, you have to call a php file, in that file you have to put your WebKey that you get from the FireBase console. that file php will receive the data sent from the html form, it will connect to your database, it will get the tokens one by one, it will communicate with the FireBase servers and will send the data to each of the devices.

  • In your class FireBaseMessagingService , you implement the method onMessageReceived which receives a parameter of type RemoteMessage . to know if everything went well enough with if(remoteMessage.getNotification()!=null) if that is true you get the data as follows:

    String titulo = remoteMessage.getNotification().getTitle();
    String contenido= remoteMessage.getNotification().getBody();
    
  • In case you have some other type of data and it does not appear in getters , you get it that way

    descuento = remoteMessage.getData().get("discount");
    

    this is a summary explanation, I leave the link on how to do it. Good luck

    FireBase, Android, php

        
    answered by 25.11.2016 в 20:06