在Phonegap和WebIntent中使用Android推送通知

时间:2013-12-05 01:46:40

标签: javascript android android-intent cordova

我已成功实施WebIntent插件,以便在收到推送通知且应用未激活时;当用户点击它时,应用程序会在索引页面打开并显示包含相关消息的模式。

这是获取显示模态消息的路径的webintent代码:

        window.plugins.webintent.hasExtra('com.parse.Data',
            function(has) {
                if (has) {
                    // has is true iff it has the extra
                    console.error('extra was gettable');
                    window.plugins.webintent.getExtra('com.parse.Data',
                        function(data) {

                            var data = JSON.stringify(data),
                                data = data.replace(/[\\]/g, '').replace(/[\/]/g, ''),
                                data = data.replace(/"(.+)"/, "$1"),
                                data = $.parseJSON(data);

                            Parse.history.navigate('notification/' + data.pid, true);

                            // if user authenticated already just route 
                            // if not authenticate then route...
                            //AppUser.authenticate();
                        }, function() {
                            // There was no extra supplied.
                            console.error('extra was NOT gettable');
                        }
                    );
                } else {
                    console.error('extra was NOT supplied');
                }
            }, function() {
                // Something really bad happened.
                console.error('Something really bad happened');
            }
        );

我正在使用Parse SDK并按照此博文发布此案例:

http://www.raymondcamden.com/index.cfm/2012/10/10/PhoneGap-Parsecom-and-Push-Notifications#c1E596902-EB43-70CE-3E0A44563B5EC611

这是我的AndroidMainfest.xml:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" android:windowSoftInputMode="adjustPan" package="com.leotech.slcompanion" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="Hello_World" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.phonegap.DroidGap" android:label="@string/app_name">
            <intent-filter>
            </intent-filter>
        </activity>
        <activity android:label="@string/app_name" android:name="com.facebook.LoginActivity" />
        <service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
</manifest>

我想知道的是,如果在应用已经有效的情况下收到推送通知时,有一种方法可以在应用中触发操作。

因此,无论用户在应用程序中的哪个位置,都应触发模态路径以显示相关消息。

1 个答案:

答案 0 :(得分:2)

您可以通过收听PhoneGap's "resume" event

来对通知作出反应
document.addEventListener("resume", function(){
  // check for your webintent and perform your action
}, true);

当应用程序位于前台时,这是有效的,尽管文档显示它只是背景。