Appcelerator:使用GCM获取通知,让该通知打开应用程序

时间:2012-07-09 16:37:32

标签: android titanium google-cloud-messaging

我已经让GCM向我的手机发送通知,而我的手机可以获取它们(如果应用已经打开。如果它已经不再运行,则无法打开。这也是一个问题。)

但是,现在我专注于点击我收到的通知时会发生什么。特别是没有任何事情发生。我已经设置好了它应该打开应用程序,通过意图,但它不起作用。

以下是我app.js的相关部分:

var senderId = 'XXXXXXXXXX';

var c2dm = require('com.findlaw.c2dm');
Ti.API.info("module is => " + c2dm);

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

        var params = {devicecode: e.registrationId, deviceType: "Android"};
        JOURNAL.webApi.webCallPOST(JOURNAL.serviceLocatorModel.urls.Membership, "/registerdevice", params, JOURNAL.registerDeviceComplete, JOURNAL.registerDeviceError);
    },
    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.geneca.journaling.GenecaJournalingActivity',
            //className: 'org.appcelerator.titanium.TiActivity',
            packageName: 'com.geneca.journaling'
        });
        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);
    }
});

这是我的tiapp.xml中的相关部分

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest>
        <activity android:name="com.geneca.journaling.GenecaJournalingActivity" />
        <permission android:name="com.geneca.journaling.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
        <uses-permission android:name="com.geneca.journaling.permission.C2D_MESSAGE"/>

        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
        <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.geneca.journaling"/>
                </intent-filter>
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                    <category android:name="com.geneca.journaling"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>
</android>
<modules>
    <module platform="android" version="0.1">com.findlaw.c2dm</module>
</modules>

我已经运行了log cat,它正在获取通知和意图,但它会吐出来,并且不会打开应用程序:

I/ActivityManager(  307): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 pkg=com.geneca.journaling cmp=com.geneca.journaling/.GenecaJournalingActivity bnds=[0,102][720,230] u=0} from pid -1
W/KeyguardViewMediator(  307): verifyUnlock called when not externally disabled
W/InputMethodManagerService(  307): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41c18038 attribute=android.view.inputmethod.EditorInfo@42319798

(中间的并不总是出现,所以我认为它并不真正相关。)

另一方面,如果应用程序未打开,则日志猫会给我Bad notification posted from package com.geneca.journaling.mobile: Couldn't create icon,然后会出现一堆错误。

1 个答案:

答案 0 :(得分:0)

我已将gcm模块上传到github。希望你们能很好地合作。 =] https://github.com/liccowee/Google-Cloud-Messaging--Titanium-