有没有办法在没有appcelerator云服务的情况下发送推送通知?

时间:2015-03-23 16:10:51

标签: push-notification titanium

我已经知道如何使用Alloy在Titanium中发送推送通知,我的方法是:

        // Require the module
    var CloudPush = require('ti.cloudpush');
    var deviceToken = null;

    // Initialize the module
    CloudPush.retrieveDeviceToken({
        success: deviceTokenSuccess,
        error: deviceTokenError
    });
    // Enable push notifications for this device
    // Save the device token for subsequent API calls
    function deviceTokenSuccess(e) {
        deviceToken = e.deviceToken;
       // alert("--->" + deviceToken);
        subscribeToChannel();
    }
    function deviceTokenError(e) {
        alert('Failed to register for push notifications! ' + e.error);
    }

    // Process incoming push notifications
    CloudPush.addEventListener('callback', function (evt) {
        alert("Notification received: " + evt.payload);
    });


    // For this example to work, you need to get the device token. See the previous section.
    // You also need an ACS user account.
    // Require in the Cloud module
    var Cloud = require("ti.cloud");

    function loginUser(){
     // Log in to ACS
        Cloud.Users.login({
            login: 'example',
            password: 'example'
        }, function (e) {
     if (e.success) {
                alert('Login successful');
            } else {
                alert('Error:\n' +
                    ((e.error && e.message) || JSON.stringify(e)));
            }
        });
    }
    function subscribeToChannel(){
     // Subscribe the user and device to the 'test' channel
     // Specify the push type as either 'android' for Android or 'ios' for iOS
     // Check if logged in:
        Cloud.PushNotifications.subscribe({
            channel: 'test',
            //device_token: 'APA91bHRjGoZLCYKwn-XcCtNLETuf-KRKfT4sMgVE4KgXQgInYfZuYTNrZC7FUMugLs0idzzqtLytrvVJjVzYBzQoc7Q81hEerq0O2vww_tV8mACuUfAi0JRvs7LoufnQZpYLZrb_1rlUsIOEMsPxDs9b_pIRJF5rw',
            device_token:deviceToken,
            type: Ti.Platform.name == 'android' ? 'android' : 'ios'
        }, function (e) {
     if (e.success) {
                alert('Subscribed');
            } else {
                alert('Error:\n' +
                    ((e.error && e.message) || JSON.stringify(e)));
            }
        });
    }
    function unsubscribeToChannel (){
     // Unsubscribes the user and device from the 'test' channel
        Cloud.PushNotifications.unsubscribe({
            channel: 'test',
            device_token: deviceToken
        }, function (e) {
     if (e.success) {
                alert('Unsubscribed');
            } else {
                alert('Error:\n' +
                    ((e.error && e.message) || JSON.stringify(e)));
            }
        });
    }



 loginUser();

然而,这种方式仅用于通过https://cloud.appcelerator.com/手动发送推送通知,因为需要您编写警报并按下该后端站点中的按钮。

所以我的问题:有没有办法以自动方式从自己的服务器发送Titanium中的推送通知?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。

如何获取推送通知的设备令牌,请参阅Titanium文档here

要发送通知,您必须将令牌发送到您的服务器。然后,服务器将您的通知发送到Apple推送通知服务(APNS)。请参阅Apple文档。那不是"自动"但对于PHP或任何其他语言来说,这是一个简单的任务 - 你可以找到很多脚本。

您还可以安排本地通知,这些通知可能会派上用场,具体取决于您的情况。

相关问题