2

我有 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,然后是一堆错误。

4

1 回答 1

0

我已将我的 gcm 模块上传到 github。希望你们可以好好工作。=] https://github.com/liccowee/Google-Cloud-Messaging--Titanium-

于 2012-08-03T05:53:05.093 回答