如何使用GSM Titanium获取移动注册ID?

时间:2014-02-06 17:24:13

标签: titanium titanium-mobile

我正在开发钛金属的Android应用程序,通过按钮点击事件我必须从GCM获得注册ID。怎么做我是钛的新手。

我遵循了http://iamyellow.net/post/40100981563/gcm-appcelerator-titanium-module,但我无法理解如何实施?

提前致谢。

1 个答案:

答案 0 :(得分:3)

要实施推送通知,您应该使用Ti.CloudPush模块。

您可以通过6个简单步骤实现GCM推送通知

  1. 设置Google Cloud Messaging
  2. 在ACS控制台中推送配置
  3. CloudPush模块实施
  4. 检索设备令牌
  5. 云用户登录
  6. 订阅频道
  7. <强> 1。设置GCM

    要使用GCM,您需要创建Google API项目以获取Google API密钥和GCM发件人ID。有关创建和获取这些项目的说明,请参阅Android Developer: Getting Started with GCM并按照“创建Google API项目”,“启用GCM服务”和“获取API密钥”中的说明进行操作。

    我们将项目编号用作 GCM发件人ID

    创建新服务器密钥时,系统会要求您提供IP地址列表以接受来自的请求。请勿在文本框中输入任何信息,然后单击“创建”以接受所有IP地址。

    <强> 2。在ACS中推送配置

    转到your apps,然后转到我的应用 - &gt;管理ACS - &gt;发展 - &gt;设置/ Android推送配置,并在Google云消息传递(GCM)发件人ID文本框中的Google云消息传递(GCM)API密钥文本框和GCM发件人ID中输入您的Google API密钥。 (我们从第1步获得)

    第3。 CloudPush模块实施

    • 将CloudPush模块添加到您的应用程序中。

      要添加CloudPush模块,您需要在TiApp.xml中添加<module platform="android">ti.cloudpush</module>。然后使用var CloudPush = require('ti.cloudpush');

    • 在您的javascript文件中使用该模块
    • 要使用推送通知,请在tiapp.xml文件中指定推送类型到GCM。

    要执行此操作,请转到tiapp.xml。

    添加/编辑以下行

    <property name="acs-push-type-development" type="string">gcm</property>
    <property name="acs-push-type-production" type="string">gcm</property>
    <property name="acs-push-type" type="string">gcm</property>
    

    <强> 4。检索设备令牌

    在将enabled属性设置为true之前调用retrieveDeviceToken方法,以使设备能够接收推送通知。您必须在任何其他CloudPush API调用之前调用retrieveDeviceToken,否则设备将不会收到推送通知。

    var CloudPush = require('ti.cloudpush');
    var deviceToken = null;
    CloudPush.retrieveDeviceToken({
        success: function deviceTokenSuccess(e) {
            Ti.API.info('Device Token: ' + e.deviceToken);
            deviceToken = e.deviceToken;
        },
        error: function deviceTokenError(e) {
            alert('Failed to register for push! ' + e.error);
        }
    });
    

    <强> 5。云用户登录

    在订阅推送通知之前,云用户应该登录。

    Cloud.Users.login({
        login: username,
        password: password
    }, function (e) {
        if (e.success) {
            alert("login success");
        } else {
            alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });  
    

    <强> 6。订阅频道

    您需要订阅频道才能获得推送。推送通知将发送到特定频道,并且将覆盖订阅该频道的所有用户。

    if(deviceToken != null){
       Cloud.PushNotifications.subscribe({
           channel: 'yourchannelName',
           device_token: deviceToken,
           type: 'gcm' //here i am using gcm, it is recommended one
       }, function (e) {
            if (e.success) {
               alert('Subscribed for Push Notification!');
           } else {
               alert('Subscribe error:' + ((e.error && e.message) || JSON.stringify(e)));
           }
       });
    } else {
       alert('You need to retrieve the device token first');
    }
    

    现在您可以发送推送通知。要执行此操作,请转到我的应用 - &gt;管理ACS - &gt;发展 - &gt;推送通知,在这里您可以看到订阅推送通知,频道等的客户数量。您可以从那里发送推送通知。

    更新:注意!!

    1. GCM支持运行Android 2.2及更高版本

    2. 的设备
    3. Google Play商店应用程序应安装在您的设备中。

    4. 对于4.0之前的设备,用户需要设置自己的Google帐户。

    5. 您正在使用Android设备进行测试,而不是模拟器(因为您无法在模拟器中安装Google Play)。

    6. Google Play服务正在您的设备上运行。

    7. 以下链接可以帮助您: