FCM Config Problem Push Notif Android & iOs in Laravel - android

I got some problems with FCM in my projects.
This is my config FCM in .env
FCM_SERVER_KEY_ANDRO=thisKeyForAndroid
FCM_SERVER_KEY_IOS=thisKeyForIOS
FCM_SENDER_ID=senderId
And this is my config fcm.php
<?php
return [
'driver' => env('FCM_PROTOCOL', 'http'),
'log_enabled' => false,
'http' => [
'server_key' => env('FCM_SERVER_KEY_ANDRO'), //the problem is here
'sender_id' => env('FCM_SENDER_ID'),
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'timeout' => 30.0, // in second
],
];
I have a problem when I use FCM_SERVER_KEY_ANDRO for server_key, push notification in IOS is not working. But when I use FCM_SERVER_KEY_IOS for server_key, push notification in IOS is working but not in Android.
I can't for adding new parameter on http for key android or key ios.
I want FCM_SERVER_KEY_ANDRO and FCM_SERVER_KEY_IOS to run concurrently on fcm.php, but i don't know how to run both?
Sorry for my bad grammar

you can use config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); for android and config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]); for iOS but you need to separate the function to make this work. you can put this code before $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
Here the example:
public function ios($data, $notifications, $token)
{
config(['fcm.http.server_key' => env('FCM_SERVER_KEY_IOS')]);
......
......
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
return response()->default(200, 'Sent', $downstreamResponse);
}
public function android($data, $notifications, $token)
{
config(['fcm.http.server_key' => env('FCM_SERVER_KEY_ANDROID')]);
......
......
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
return response()->default(200, 'Sent', $downstreamResponse);
}

Related

How to increase Post Views Counter plugin's views count using the REST API?

I am making an Android app using Rest API and getting data from my WordPress website.
I have implemented a post views functionality on my app and I am using the Post Views Counter plugin on my WordPress website. Now the problem is that when I browse post on desktop the number of post views is increasing but on my Android app when I open a post it doesn’t increase.
Is there any method that I can use in my Android app to increase post view as well?
I have tried this code i found on internet but not working:
add_action( 'rest_api_init', function () {
register_rest_route( 'base', '/views/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'post_view_counter_function',
));
});
function post_view_counter_function( WP_REST_Request $request ) {
$post_id = $request['id'];
if ( FALSE === get_post_status( $post_id ) ) {
return new WP_Error( 'error_no_post', 'Not a post id', array( 'status' => 404 ) );
} else {
$current_views = get_post_meta( $post_id, 'views', true );
$views = $current_views + 1;
update_post_meta( $post_id, 'views', $views );
return $views;
}
}
please help

Using Ruby to send notification to Firebase

I am using Ruby version 1.8.7.
I use this FCM gem https://github.com/spacialdb/fcm and want to send notification message to Android client app but it does not work.
In Controller:
fcm = FCM.new(FIREBASE_API_KEY, :timeout => 30)
options = {:data => {:message => "This is a FCM Topic Message!"}}
response = fcm.send_to_topic('global', options)
Class FCM:
require 'httparty'
require 'cgi'
require 'json'
class FCM
include HTTParty
base_uri 'https://fcm.googleapis.com/fcm'
default_timeout 30
format :json
attr_accessor :timeout, :api_key
def initialize(api_key, client_options = {})
#api_key = api_key
#client_options = client_options
end
def send_with_notification_key(notification_key, options = {})
body = { :to => notification_key }.merge(options)
params = {
:body => body.to_json,
:headers => {
'Authorization' => "key=#{#api_key}",
'Content-Type' => 'application/json'
}
}
response = self.class.post('/send', params.merge(#client_options))
response.parsed_response
end
def send_to_topic(topic, options = {})
if topic =~ /[a-zA-Z0-9\-_.~%]+/
send_with_notification_key('/topics/' + topic, options)
end
end
end
The server key is correct, because I can send notification successfully by PHP code.
The response output as below:
{"message_id"=>8885803884270587181}
Could anyone please to point out what wrong with the code.
Any help would be greatly appreciated.
According to the Firebase API documentation the response you get is the expected response for a successfully queued message.
The fact that you get back a message_id has this meaning:
The topic message ID when FCM has successfully received the request and will attempt to deliver to all subscribed devices.
It looks like your code is working, i.e. the problem must be somewhere else.
EDIT:
You are sending a data message. (Because no notification key, just a data key) Perhaps your client expects a notification message instead?
See the documentation for the distinction between those two message types.
You can try and just add a notification key to the request:
fcm = FCM.new(FIREBASE_API_KEY, :timeout => 30)
options = {:notification => "Test notification",
:data => {:message => "This is a FCM Topic Message!"}}
response = fcm.send_to_topic('global', options)
I have this problem before
try to add priority: "high" and notification: "your message"
in your FCM class instatiation options
I am not sure if this is due to changes in FCM itself but using the syntax from the gem's documentation or using the format from Daniel's answer did not work for me (Daniel's version gives an error response from FCM saying that notification must be a JSON object).
This is what worked for me:
fcm.send_to_topic(topic, notification: { body: "topic notification" })
Unfortunately the fcm library does not provide support to the rubies < 2.0. According to the git history of the repository that was the case already at the start of the project.
Try this
request = HTTParty.post('http://fcm.googleapis.com/fcm/send', :body => { "to" => "#{token}", "priority" => "high", "data" => { "title" =>title,"body"=>message,'massage_type'=>'text'}}.to_json, :headers => { 'Content-Type' => 'application/json', 'Authorization' => "key=#{server_token}" } )

Open a specific view in ionic app when user taps on push notification

I know this question has been asked and answered many times but none of the solution is working for me.
I have followed ionic.io documentation https://docs.ionic.io/services/push/ to implement push notification and it works fine when app is in foreground. I am also able to receive notification when app is closed. Now when user clicks on that notification, I want to open a specific view.
As per documentation, To handle push notifications in your app, we need to listen to the cloud:push:notification event using angular’s $on.
$scope.$on('cloud:push:notification', function(event, data) {
var msg = data.message;
alert(msg.title + ': ' + msg.text);
});
This code is working fine when app is foreground. But when app is closed and user opens the app by tapping the push notification, I want to open specific view/controller.
I have placed the above code in .run function and outside $ionicPlatform.ready function.
Here is my code to call FCM Rest service
function sendFCMNotification($request_data){
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
//The device token.
$token = $request_data['device_id'];
//Title of the Notification.
$title = $request_data['title'];
//Body of the Notification.
$body = $request_data['body'];
//Creating the notification array.
$notification = array('title' =>$title , 'body' => $body,'content-available'=> '1');
//This array contains, the token and the notification. The 'to' attribute stores the token.
$arrayToSend = array('to' => $token, 'notification' => $notification);
//Generating JSON encoded string form the above array.
$json = json_encode($arrayToSend);
//Setup headers:
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key= {MY GCM KEY}';
//Setup curl, add headers and post parameters.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
curl_exec($ch);
//Close request
curl_close($ch);
}
Can anyone help me in achieving this?
Pushwoosh provides a method which can tell us if the app has been launched by clicking push notification or not.
https://rawgit.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin/master/Documentation/files/PushNotification-js.html#PushNotification.getLaunchNotification
Is there any similar function in ionic push plugin?
If you are sending a CURL req to ionic for push use this data structure
$notficationHolder =array(
"user_ids" => array(),
"profile" => "push",
"notification"=>array(
"android"=>array(
"title"=>'TITLE',
"message"=>"You have a notification",
"sound" => "sound",
"icon_color" => "#FA2B2E",
"icon" => "notification",
/* "image" => "https://pbs.twimg.com/profile_images/617058765167329280/9BkeDJlV.png", */
"payload" => array(
'$state'=> 'main.myProfile',
'$stateParams'=> array()
)
),
"ios"=>array(
"sound" => "default",
"payload" => array(
'$state'=> 'main.myProfile',
'$stateParams'=> array()
)
)
)
);
This is an array. json encode it and curl it to ionic. The catch is your payload property in the notification object.
you need to add in payload attribute
{"$state":'yourStateName','$stateParams':{'paramOne':1,'paramTwo':2}}

How can echo a json data after an email sent in codeigniter

I am using codeigniter email, to send an email to the client who has registerd him self into the system, and after that I echo a json code that syas an email has been sent to you.
this proccess is working when i am using web browser, but it is not working in android application.
my code is below:
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.1and1.com',
'smtp_port' => 587,
'smtp_user' => $from_email,
'smtp_pass' => $this->config->item('email_password'),
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from($from_email, "something");
$this->email->reply_to($from_email, "something");
$this->email->to($to_email);
$this->email->subject("Account Verification");
$this->email->set_mailtype("html");
$this->email->message($email_body);
if($this->email->send()){
$rsp['error'] = 0;
$rsp['data'] = $user;
echo json_encode($rsp);}else{do some thing...}
but if i remove the $this->email->send() from code, then the echo json code works fine, but not emil sendig.
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.1and1.com',
'smtp_port' => 587,
'smtp_user' => $from_email,
'smtp_pass' => $this->config->item('email_password'),
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from($from_email, "something");
$this->email->reply_to($from_email, "something");
$this->email->to($to_email);
$this->email->subject("Account Verification");
$this->email->set_mailtype("html");
$this->email->message($email_body);
$this->email->send();
$rsp['error'] = 0;
$rsp['data'] = $user;
echo json_encode($rsp);
like this try once
Your code:
if ($this->email->send()) {
echo json_encode($rsp);
} else {
// do some thing...
}
means that JSON will be printed only if send() function return true. If it returns false, then the logic will go to else section and no JSON will be printed (unless you modify // do some thing to print JSON also).
I'm pretty sure that mail() return false. You are using CI, don't forget to check application/logs/*.log to see the error detail.

The push notification format of Pushwoosh and Phonegap

Now the pushwoosh works without problem. I can see the notification on the upper left corner of the Android phone. How can I set the notification to be pop-up onto screen rather than the notification on upper left only, even the phone is in silence mode!
Which way should I go for the customization? Thx
function pwCall( $action, $data = array() ) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $action;
$json = json_encode( array( 'request' => $data ) );
$res = doPostRequest( $url, $json, 'Content-Type: application/json' );
print_r( #json_decode( $res, true ) ); }
pwCall( 'createMessage', array( '
application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => array(
array(
'send_date' => 'now',
'content' => 'test',
'ios_badges' => 3,
'data' => array( 'custom' => 'json data' ),
'link' => 'http://pushwoosh.com/'
)
)
)
);
To configure how to display the incoming push notification, you should write your own logic in the receiver in Android app, as specified in this page.
Currently, the tutorial currently uses the following the show the message:
private void showMessage(String message)
{
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
Try to create your own logic (user interface, popup method & timing, etc) in the function.

Categories

Resources