douhuan1979 2014-10-03 11:29 采纳率: 100%
浏览 46
已采纳

使用phonegap构建在iOS上推送通知,没有第三方服务

So I have this client who is not willing to pay for 3rd party services like PushWoosh to handle push notifications and I need to implement them by using this plugin: https://github.com/phonegap-build/PushPlugin on Phonegap Build

Here is what i have so far:

A PHP file that is supposed to send the notifications (found this in a partial tutorial)

<?php
// Set parameters:
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

// Setup stream:
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

// Open connection:
$apns = stream_socket_client(
    'ssl://' . $apnsHost . ':' . $apnsPort,
    $error,
    $errorString,
    2,
    STREAM_CLIENT_CONNECT,
    $streamContext
);

// Get the device token (fetch from a database for example):
$deviceToken = '...';

// Create the payload:
$message = 'Hallo iOS';
// If message is too long, truncate it to stay within the max payload of 256 bytes.
if (strlen($message) > 125) {
    $message = substr($message, 0, 125) . '...';
}

$payload['aps'] = array('alert' => $message, 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);

// Send the message:
$apnsMessage
    = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload))
    . $payload;
// fwrite($apns, $apnsMessage);

// Close connection:
@socket_close($apns);
fclose($apns);
?>

A JS code that I am supposed to add in the app (I think) also found in that partial tutorial:

// Setup push notifications:
try
{
    var pushNotification = window.plugins.pushNotification;
    if (window.device.platform == 'iOS') {
        // Register for IOS:
        pushNotification.register(
            pushSuccessHandler,
            pushErrorHandler, {
                "badge":"true",
                "sound":"true",
                "alert":"true",
                "ecb":"onNotificationAPNS"
            }
        );
    }
}
catch(err)
{
    // For this example, we'll fail silently ...
    console.log(err);
}

/**
 * Success handler for when connected to push server
 * @param result
 */
var pushSuccessHandler = function(result)
{
    console.log(result);
};

/**
 * Error handler for when not connected to push server
 * @param error
 */
var pushErrorHandler = function(error)
{
    console.log(error);
};

/**
 * Notification from Apple APNS
 * @param e
 */
var onNotificationAPNS = function(e)
{
    // ...
};

And a file that should insert device tokens in the database that I will create. I am supposed to call this file as: ../deviceAdd.php?token=XXXXXXX

The problem is I HAVE NO IDEA how to get that device token to pass to the deviceAdd file.

Any help is highly appreciated!

  • 写回答

1条回答 默认 最新

  • dongsi0625 2014-10-06 16:47
    关注

    Found the answer. Here it is: My register should be like this:

    pushNotification.register(
            tokenHandler,
            errorHandler,
            {
                "badge":"true",
                "sound":"true",
                "alert":"true",
                "ecb":"onNotificationAPN"
            });
    

    and my tokenHandler function should be:

    function tokenHandler (result) {
        // Your iOS push server needs to know the token before it can push to this device
        // here is where you might want to send it the token for later use.
        alert('device token = ' + result);
        //insertToken();
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据