首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Appcelerator:使用GCM获取通知,让通知打开应用程序

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

Stack Overflow用户
提问于 2012-07-10 00:37:32
回答 1查看 3.2K关注 0票数 2

我有GCM发送通知到我的手机,我的手机可以得到它们(如果应用程序已经打开。如果它不再运行,它就不会打开。这也是一个问题。)

然而,现在我关注的是当我点击我收到的通知时会发生什么。特别是,什么都不会发生。我已经将其设置为通过intents打开应用程序,但它不起作用。

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

代码语言:javascript
复制
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中的相关部分

代码语言:javascript
复制
<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,它得到了通知和意图,但它吐出了这个,并且没有打开应用程序:

代码语言:javascript
复制
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,然后会出现一系列错误。

EN

回答 1

Stack Overflow用户

发布于 2012-08-03 13:53:05

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11399404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档