Kuraturpa / titanium-c2dm

C2dm push notifications for titanium-mobilesdk 1.8.0.1
Other
18 stars 3 forks source link

No method 'register' #4

Closed matthewphewes closed 12 years ago

matthewphewes commented 12 years ago

I keep getting an error when trying to register for C2DM notifications on the device...

Error: Uncaught Type Error: Object # has no method 'register'

I successfully compiled the module using 1.8.2 and it loads but when I try to do:

        var senderId = 'email@domain.com';

        // the module is loaded earlier in the app under 'namespace.c2dm'
    Ti.API.info("module is => " + namespace.c2dm);

    Ti.API.info('Registering...');
    namespace.c2dm.register(senderId, {
        success:function(e)
        {
            Ti.API.info('JS registration success event: ' + e.registrationId);

            Ti.API.info('Requesting: '+peak.config.rest_server+'device/register');
            alert(e.registrationId);
        },
        error:function(e)
        {
            Ti.API.error("Error during registration: "+e.error);

            var message;
            if(e.error == "ACCOUNT_MISSING") {
                message = "No Google account found; you'll need to add one (in Settings/Accounts) in order to activate notifications";
            } else {
                message = "Error during registration: "+e.error
            }

            Titanium.UI.createAlertDialog({
                title: 'Push Notification Setup',
                message: message,
                buttonNames: ['OK']
            }).show();
        },
        callback:function(e) // called when a push notification is received
        {
            Ti.API.info('JS message event: ' + JSON.stringify(e.data));

            var intent = Ti.Android.createIntent({
                action: Ti.Android.ACTION_MAIN,
                flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED,
                className: 'com.company.app.YourActivity',
                packageName: 'com.company.app'
            });
            intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);

            // This is fairly static: Not much need to be altered here
            var pending = Ti.Android.createPendingIntent({
                activity: Ti.Android.currentActivity,
                intent: intent,
                type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
            });

            var notification = Ti.Android.createNotification({
                contentIntent: pending,
                contentTitle: 'New message',
                contentText: e.data.message,
                tickerText: "New message"
            });

            Ti.Android.NotificationManager.notify(1, notification);
        }
    });
 <android xmlns:android="http://schemas.android.com/apk/res/android">
        <tool-api-level>8</tool-api-level>
        <manifest>
            <permission android:name="com.uamsmhewes.peaktest.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
            <uses-permission android:name="com.uamsmhewes.peaktest.permission.C2D_MESSAGE"/>
            <uses-permission android:name="android.permission.USE_CREDENTIALS" />
            <uses-permission android:name="android.permission.WAKE_LOCK" />
            <uses-permission android:name="android.permission.CAMERA" />
            <uses-permission android:name="android.permission.FLASHLIGHT" />
            <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
            <uses-permission android:name="com.google.android.c2dm.permission.REGISTER"/>
            <uses-permission android:name="android.permission.WAKE_LOCK"/>

            <application>
                <service android:name="com.findlaw.c2dm.C2DMReceiver"/>
                <receiver
                    android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 
                        android:permission="com.google.android.c2dm.permission.SEND">
                    <intent-filter>
                        <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                        <category android:name="com.uamsmhewes.peaktest"/>
                    </intent-filter>
                    <intent-filter>
                        <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                        <category android:name="com.uamsmhewes.peaktest"/>
                    </intent-filter>
                </receiver>
                <activity android:name="com.mwaysolutions.barcode.TitaniumBarcodeActivity"
                    android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
                    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
            </application>

            <supports-screens android:smallScreens="false"
                android:normalScreens="true" android:largeScreens="true"
                android:anyDensity="false" />

        </manifest>
</android>

Any help is greatly appreciated! Thanks!

Kuraturpa commented 12 years ago

Hi The method name is registerC2dm

matthewphewes commented 12 years ago

Thanks!