Android Question Detect click from notification

Stevenindonesia

Member
Licensed User
How can i detect user launch application by notification?
I am using B4X and i tried below code :

B4X:
Sub Process_Globals
    Private LastIntent As Intent
End Sub

Sub Activity_Resume
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in.HasExtra("Notification_Tag") And in <> LastIntent Then
        LastIntent = in
        Dim tag As String = in.GetExtra("Notification_Tag")
        Msgbox("User clicked notification: " & tag,"Message")
    End If
End Sub

I still couldn't capture the intent and message did not pop up.
 

Stevenindonesia

Member
Licensed User
Erel,

My application are able to get notification. But when i click on the notification... my application will launch. How can i detect that the user launch the application by clicking on notification?
What i wanted to do is - when user launch application by clicking in coming notification, I will show the notification page.


B4X:
Sub Process_Globals
    Private LastIntent As Intent
End Sub

Sub Activity_Resume
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in.HasExtra("Notification_Tag") And in <> LastIntent Then
        LastIntent = in
        Dim tag As String = in.GetExtra("Notification_Tag")
        Log("User clicked notification: " & tag)
    End If
End Sub

The above code doesn't seems to work.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Erel asked you to post the code that generates the notification.

Where is that code?

You just posted the same code as in #1 of this thread.
 
Upvote 0

Stevenindonesia

Member
Licensed User
Erel, DonManfred,
We send the notification using PHP. Please find below code the JSON part that sends to Firebase :
B4X:
//**************************************************
//  SEND NOTIFICATION
//**************************************************
function sendNotificationToTopic($OS,$iprojectId,$itoken,$itopic, $ititle, $imessage){
    if($OS=="Android"){
        $data = [
            "message" => [
                "topic" => "AND_".$itopic,
                "data" => [
                    "title" => $ititle,
                    "body" => $imessage
                ],
                "apns" => [
                    "payload" => [
                        "aps" => [
                            "sound" => "default"
                        ]
                    ]
                ]
            ]
        ];
    }elseif($OS=="IOS"){
        $data = [
            "message" => [
                "topic" => "IOS_".$itopic,
                "notification" => [
                    "title" => $ititle,
                    "body" => $imessage
                ],
                "apns" => [
                    "payload" => [
                        "aps" => [
                            "sound" => "default"
                        ]
                    ]
                ]
            ]
        ];
    }
    $url = "https://fcm.googleapis.com/v1/projects/" . $iprojectId . "/messages:send";
    $headers = [
        "Authorization: Bearer " . $itoken,
        "Content-Type: application/json"
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    $response = curl_exec($ch);
    if (curl_error($ch)) {
        die("CURL Error: " . curl_error($ch));
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    echo "HTTP Code: " . $httpCode . "\n";
    echo "Response: " . $response . "\n";
}


The notification arrived in both Android and IOS but when i click the notification, i couldnt 'catch' GetStartingIntent for both Android and IOS
 
Upvote 0
Top