android - Appcelerator : Use GCM to get a notification, 有通知打开应用程序

标签 android titanium google-cloud-messaging

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

但是,现在我关注的是当我点击收到的通知时会发生什么。特别是,没有任何反应。我已将其设置为通过 intent 打开应用程序,但它无法正常工作。

这是我的 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,它收到了通知和 Intent ,但它吐出了这个,并且没有打开应用程序:

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 然后一堆的错误。

最佳答案

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

关于android - Appcelerator : Use GCM to get a notification, 有通知打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11399404/

相关文章:

java - 如何只保存新的高分并放入排行榜

java - Android:当url包含http时,在设置WebViewCore之前不支持removeMessages(int what = 107)

javascript - 钛的Paypal集成问题

android - "if the device is connected to GCM"是什么意思

android - 我可以删除此权限吗? (在 Android 5.0 设备中会导致 INSTALL_FAILED_DUPLICATE_PERMISSION)

android - 如何从 Play 商店中删除应用程序?

android.permission.INTERNET 不工作

android - HTML5 移动应用程序是否在服务器上运行并通过各种网络浏览器访问?

titanium - 如何使用钛在应用程序中保存数据或一组列表?

java - 如何重置设备的 Google Cloud Messaging 注册?