Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

How set action on Closed App #13

Closed
loreberti89 opened this issue Oct 5, 2016 · 4 comments
Closed

How set action on Closed App #13

loreberti89 opened this issue Oct 5, 2016 · 4 comments

Comments

@loreberti89
Copy link

loreberti89 commented Oct 5, 2016

Hello,
I would like to ask if I can set equivalent of .addAction() and .setStyle() of notification or how use app notification builder on closed app.
Because my problem is that when my app is closed does not execute code in onMessageReceived and I can't notification.setStyle or .addAction().

thank you for share!

@loreberti89 loreberti89 changed the title How set action on Background App How set action on Closed App Oct 5, 2016
@brozot
Copy link
Owner

brozot commented Oct 5, 2016

Hi,

For Firebase message notification does not support special notifications, use a data message because it call every time onMessageReceived.

$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData(['a_data' => 'my_data']);

maybe this thread can be help you : http://stackoverflow.com/questions/38504078/firebase-expandable-notification-show-image-when-app-is-in-background.

I hope that will help you . Please give me a feedback if that solve or not your issue.

Best regards

Nicolas

@loreberti89
Copy link
Author

Hello, thankyou for your reply!

But my problem persist.

public function alert($text = null, $token, $action=null)
    {
$optionBuiler = new OptionsBuilder();
        $optionBuiler->setTimeToLive(60*20);
        $notificationBuilder = new PayloadNotificationBuilder('NOTIFY');
        $notificationBuilder->setBody($text)
            ->setSound('default');

        $dataBuilder = new PayloadDataBuilder();

        $dataBuilder->addData(['a_data' => 'my_data']);

        $notification = $notificationBuilder->build();
        $data = $dataBuilder->build();
        $option = $optionBuiler->build();

        FCM::sendTo($token, $option, $notification, $data);
}

I have this function, and when the app is open pass on onMessageReceived corretly, but when I close my app the onMessageReceived is not executed.

am I doing something wrong?

@brozot
Copy link
Owner

brozot commented Oct 6, 2016

Hello, Try to remove the notification payload of your message like bellow:

public function alert($text = null, $token, $action=null)
    {
        $optionBuiler = new OptionsBuilder();
        $optionBuiler->setTimeToLive(60*20);
        $dataBuilder = new PayloadDataBuilder();

        $dataBuilder->addData(['a_data' => 'my_data']);

        $data = $dataBuilder->build();
        $option = $optionBuiler->build();

        FCM::sendTo($token, $option, null, $data);
}

because when notification is present on the payload, android will handle the message and show it without passing in onMessageReceived when app is in background.

From FCM documentation :

Messages with both notification and data payloads

App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a message object with both payloads available.
Here is a JSON-formatted message containing both the notification key and the data key:

  {
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

Please could you test it and give me a feedback ?

@loreberti89
Copy link
Author

Yes! it Work!
thank you!

@brozot brozot closed this as completed Oct 6, 2016
actmkan pushed a commit to actmkan/Laravel-FCM that referenced this issue Mar 3, 2021
actmkan pushed a commit to actmkan/Laravel-FCM that referenced this issue Mar 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants