ios下调用jpush的api进行推送(Rails做服务端)

 

第一步,在Gemfile里添加

gem ' jpush-api-ruby-client'

gem 'rest-client'

gem 'json'

 

第二步,下载jpush的sdk,添加到客户端的代码中,并进行配置。

在Build Settings 里搜索Search关键字,然后找到Library Search Paths,将lib文件的路径写进去。

 

第三步,APService设置

 

//设置推送类型

[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

                                                   UIRemoteNotificationTypeSound |

                                                   UIRemoteNotificationTypeAlert)];

//初始化APService

 

 [APService setupWithOption:launchOptions];

 

//接到非APNS推送时的回调

 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkDidReceiveMessage:) name:kAPNetworkDidReceiveMessageNotification object:nil];

 

//接到非APNS通知的时候将通知上传到jpush服务器

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

     [APService handleRemoteNotification:userInfo];

 

}

 

//在获取设备deviceToken的回调里将devoiceToken上传到jpush服务器

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

    [APService registerDeviceToken:deviceToken];

 

}

 

第四步、服务端代码的编写

 

    app_key = '3b1c94e4c........' //在你的jpush注册的app当中

    master_secret = '09520a3e9.......' //在你的jpush注册的app当中

    time_to_live = 60 * 60 * 24//存活时间

    jpush_client = JPushApiRubyClient::Client.new(app_key, master_secret, 'platform' => JPushApiRubyClient::PlatformType::IOS)//针对ios平台

 

    send_no = jpush_client.generate_send_no //生成一个通知

    msg_title = '通知'

 

    msg_content = message_content

//以别名的方式进行发送

    puts send_result = jpush_client.send_notification_with_alias(send_no,friend, msg_title, msg_content)

 

发送方式解释:

msg_content = '根据 appkey 发送广播通知'
   puts send_result = jpush_client . send_notification_with_appkey ( send_no , msg_title , msg_content )
 
=begin
带上可选参数调用 opts参数可包含 builder_id 、extras 、override_msg_id
puts send_result = jpush_client.send_notification_with_appkey(send_no,
msg_title,
msg_content,
'builder_id' => 0,
'extras' => {'key1'=> 'value1','key2'=>'value2'},
'override_msg_id' => '这里输入你上一条消息的msg_id')
=end
 
   msg_content = '根据 tag 发送通知'
   puts send_result = jpush_client . send_notification_with_tag ( send_no , '这里输入你的tag' , msg_title , msg_content )
 
   msg_content = '根据 alias 发送通知'
   puts send_result = jpush_client . send_notification_with_alias ( send_no , '这里输入你的alias' , msg_title , msg_content )
 
   msg_content = '根据 imei 发送通知'
   puts send_result = jpush_client . send_notification_with_imei ( send_no , '这里输入你的imei' , msg_title , msg_content )
 
 
   msg_content = '根据 appkey 发送自定义消息'
   puts send_result = jpush_client . send_message_with_appkey ( send_no , msg_title , msg_content )
=begin
带上可选参数调用 opts参数可包含 extras 、override_msg_id
puts send_result = jpush_client.send_message_with_appkey(send_no,
msg_title,
msg_content,
'extras' => {'key1'=> 'value1','key2'=>'value2'},
'override_msg_id' => '这里输入你上一条消息的msg_id')
=end
 
   msg_content = '根据 tag 发送自定义消息'
   puts send_result = jpush_client . send_message_with_alias ( send_no , '这里输入你的tag' , msg_title , msg_content )
 
   msg_content = '根据 alias 发送自定义消息'
   puts send_result = jpush_client . send_message_with_tag ( send_no , '这里输入你的alias' , msg_title , msg_content )
  msg_content = '根据 imei 发送自定义消息'
   puts send_result = jpush_client . send_message_with_imei ( send_no , '这里输入你的imei' , msg_title , msg_content )

 

 tag和alias的解释

alias下面可以有很多的tag这样我们能够批量发送通知。

 详细见

https://github.com/jpush/jpush-api-ruby-client/blob/master/example/client_example.rb

http://docs.jpush.cn/display/dev/API%3A+iOS

你可能感兴趣的:(Rails)