PhoneGap的PushNotification打开特定的应用程序页面应用程序、页面、PhoneGap、PushNotification

2023-09-04 03:08:48 作者:最怕的是自己是个多余的

我做pushnotification为安卓/ IOS.I使用了PhoneGap的推插件https://github.com/phonegap-build/PushPlugin,它似乎工作太棒了。

我在我的设备上接收消息,现在的问题是,当我点击接收到的消息是进入应用程序的index.html page.But我想打开someother页例如home.html的,当我点击了邮件和家庭。 HTML我将展示的消息。

如何实现这一目标?

MyPhoneGapActivity.java

  com.test包;

进口org.apache.cordova.DroidGap;
进口android.os.Bundle;

公共类MyPhoneGapActivity扩展DroidGap {
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        super.setIntegerProperty(启动画面,R.drawable.splash);
        super.setIntegerProperty(loadUrlTimeoutValue,60000);
        super.loadUrl(文件:///android_asset/www/index.html,10000);
    }
}
 

index.js

 <脚本类型=文/ JavaScript的>
    VAR pushNotification;

    传播onDeviceReady(){
        $(#应用程序状态-UL)追加('<李>接收到的LT deviceready事件; /李>');

        document.addEventListener(后退按钮,功能(五)
        {
            $(#应用程序状态-UL)追加('<李>接收到的LT后退按钮事件; /李>');

            如果($(#家)长度GT; 0)
            {
                //调用这个每次得到一个新的令牌。不要把它复用现有的令牌。
                //pushNotification.unregister(successHandler,的ErrorHandler);
                即preventDefault();
                navigator.app.exitApp();
            }
            其他
            {
                navigator.app.backHistory();
            }
        }, 假);

        尝试
        {
            pushNotification = window.plugins.pushNotification;
            如果(device.platform =='机器人'|| device.platform =='机器人'){
                $(#应用程序状态-UL)追加('<李>注册机器人< /李>');
                pushNotification.register(successHandler,的ErrorHandler,{senderID:661780372179,ECB:onNotificationGCM}); // 需要!
            } 其他 {
                $(#应用程序状态-UL)追加('<李>注册的iOS< /李>');
                pushNotification.register(tokenHandler,的ErrorHandler,{徽章:真正的,声音:真正的,警告:真正的,欧洲央行:onNotificationAPN}); // 需要!
            }
        }
        赶上(错误)
        {
            TXT =有这个页面上的错误。\ n \ñ。
            TXT + =错误描述:+ err.message +\ñ\ N的;
            警报(TXT);
        }
    }

    //处理为iOS APNS通知
    功能onNotificationAPN(五){
        如果(e.alert){
             $(#应用程序状态-UL)追加('<李>推通知:+ e.alert +'< /李>');
             navigator.notification.alert(e.alert);
        }

        如果(e.sound){
            VAR SND =新媒体(e.sound);
            snd.play();
        }

        如果(e.badge){
            pushNotification.setApplicationIconBadgeNumber(successHandler,e.badge);
        }
    }

    //为Android手柄GCM通知
    功能onNotificationGCM(五){
        $(#应用程序状态-UL)追加('<李>事件 - >收到:+ e.event +'< /李>');

        开关(e.event)
        {
            案注册:
            如果(e.regid.length大于0)
            {
                $(#应用程序状态-UL)追加('<李>注册 - > REGID:'+ e.regid +< /李>);
                //你的GCM推送服务器需要知道REGID才可以推到该设备
                //这里你可能会想将它发送REGID以备后用。
                的console.log(REGID =+ e.regID);
            }
            打破;

            案消息:
                //如果设置了此标志,当我们在前台此通知发生。
                //你可能想播放声音来获得用户的关注,扔了一个对话框,等等。
                如果(e.foreground)
                {
                    $(#应用程序状态-UL)追加('<李>  -  INLINE NOTIFICATION--'+'< /李>');

                    //如果通知包含soundname,玩它。
                    VAR my_media =新媒体(/ android_asset / WWW /+ e.soundname);
                    my_media.play();
                }
                其他
                {//否则,我们推出了因为用户感动的通知托盘的通知。
                    如果(e.coldstart)
                        $(#应用程序状态-UL)追加('<李>  -  COLDSTART NOTIFICATION--'+'< /李>');
                    其他
                    $(#应用程序状态-UL)追加('<李>  - 背景NOTIFICATION--'+'< /李>');
                }

                $(#应用程序状态-UL)追加('<李>消息 - >味精:'+ e.payload.message +'< /李>');
                $(#应用程序状态-UL)追加('<李>消息 - > MSGCNT:'+ e.payload.msgcnt +'< /李>');
            打破;

            案错误:
                $(#应用程序状态-UL)追加('<李>错误 - >味精:'+ e.msg +'< /李>');
            打破;

            默认:
                $(#应用程序状态-UL)追加('<李>事件 - >未知,收到一个事件,我们不知道它是为&lt什么; /李>');
            打破;
        }
    }

    功能tokenHandler(结果){
        $(#应用程序状态-UL)追加('<李>令牌:+结果+'< /李>');
        //你的iOS推送服务器需要知道令牌才可以推到该设备
        //这里是你可能想将它传送令牌以备后用。
    }

    功能successHandler(结果){
        $(#应用程序状态-UL)追加('<李>的成功:+结果+'< /李>');
    }

    功能的ErrorHandler(错误){
        $(#应用程序状态-UL)追加('<李>错误:'+误差+'< /李>');
    }

    document.addEventListener(deviceready',onDeviceReady,真正的);

 < / SCRIPT>
 

解决方案 Windows Phone Windows 8 Push Notification from Windows Azure

您可以轻松地做到这一点,

第1步:打开你的GCMIntentService.java文件

检查方法的onMessage

在该方法中,你会发现从这里,他们是路过的意图,你MyPhoneGapActivity.java ......现在,你需要做的就是你传递一些额外的信息.... 传递一些额外的信息,意图

检查此行

 意图notificationIntent =新的意图(背景下,MyPhoneGapActivity.class);
//在这里传递的信息
notificationIntent.putExtra(URL,网址);
 

您可以在上面看到我已经通过了额外的信息,网址...为完整的code见下图

 保护无效的onMessage(上下文的背景下,意图意图){
    Log.d(TAG的onMessage  - 背景:+上下文);

    //从信息中提取的有效载荷
    捆绑额外= intent.getExtras();
    如果(临时演员!= NULL)
    {
        布尔前景= this.isInForeground();

        extras.putBoolean(前台,前景);

        如果(前景){
            PushHandlerActivity.sendToApp(临时演员);
        }其他{
            字符串消息= extras.getString(信息);
            字符串标题= extras.getString(标题);
            注意notif =新的通知(android.R.drawable.btn_star_big_on,消息,System.currentTimeMillis的());
            notif.flags = Notification.FLAG_AUTO_CANCEL;
            notif.defaults | = Notification.DEFAULT_SOUND;
            notif.defaults | = Notification.DEFAULT_VIBRATE;

            字符串URL =通知;

            意图notificationIntent =新的意图(背景下,MyPhoneGapActivity.class);
            //在这里传递的信息
            notificationIntent.putExtra(URL,网址);
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);

            notif.setLatestEventInfo(背景下,产权,信息,contentIntent);
            字符串NS = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager =(NotificationManager)context.getSystemService(NS);
            mNotificationManager.notify(1,notif);
        }
    }
  }
 

步骤2:

在你MyPhoneGapActivity.java

  com.test包;

进口org.apache.cordova.DroidGap;

进口android.os.Bundle;

公共类MyPhoneGapActivity扩展DroidGap {
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        super.setIntegerProperty(启动画面,R.drawable.splash);
        super.setIntegerProperty(loadUrlTimeoutValue,60000);

        捆绑额外= getIntent()getExtras()。
        字符串消息= extras.getString(URL);
        如果(消息==通知){
            super.loadUrl(文件:///android_asset/www/home.html,10000);
        }其他{
            super.loadUrl(文件:///android_asset/www/index.html,10000);
        }
    }
}
 

这就是现在,当您单击该通知,将加载Home.html文件中: - )

I am doing pushnotification for both Android/IOS.I have used a phonegap push-plugin https://github.com/phonegap-build/PushPlugin, it seems to work great.

I am receiving message on my device now the problem is When i click on the received message it goes to app index.html page.But i want to open someother page eg home.html when i clicked the message and in home.html i will be showing the message.

How to achieve this?

MyPhoneGapActivity.java

package com.test;

import org.apache.cordova.DroidGap;
import android.os.Bundle;

public class MyPhoneGapActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setIntegerProperty("splashscreen", R.drawable.splash);
        super.setIntegerProperty("loadUrlTimeoutValue", 60000); 
        super.loadUrl("file:///android_asset/www/index.html", 10000);
    }
}

index.js

<script type="text/javascript">
    var pushNotification;

    function onDeviceReady() {
        $("#app-status-ul").append('<li>deviceready event received</li>');

        document.addEventListener("backbutton", function(e)
        {
            $("#app-status-ul").append('<li>backbutton event received</li>');

            if( $("#home").length > 0)
            {
                // call this to get a new token each time. don't call it to reuse existing token.
                //pushNotification.unregister(successHandler, errorHandler);
                e.preventDefault();
                navigator.app.exitApp();
            }
            else
            {
                navigator.app.backHistory();
            }
        }, false);

        try 
        { 
            pushNotification = window.plugins.pushNotification;
            if (device.platform == 'android' || device.platform == 'Android') {
                $("#app-status-ul").append('<li>registering android</li>');
                pushNotification.register(successHandler, errorHandler, {"senderID":"661780372179","ecb":"onNotificationGCM"});     // required!
            } else {
                $("#app-status-ul").append('<li>registering iOS</li>');
                pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});    // required!
            }
        }
        catch(err) 
        { 
            txt="There was an error on this page.\n\n"; 
            txt+="Error description: " + err.message + "\n\n"; 
            alert(txt); 
        } 
    }

    // handle APNS notifications for iOS
    function onNotificationAPN(e) {
        if (e.alert) {
             $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
             navigator.notification.alert(e.alert);
        }

        if (e.sound) {
            var snd = new Media(e.sound);
            snd.play();
        }

        if (e.badge) {
            pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
        }
    }

    // handle GCM notifications for Android
    function onNotificationGCM(e) {
        $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

        switch( e.event )
        {
            case 'registered':
            if ( e.regid.length > 0 )
            {
                $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
                // Your GCM push server needs to know the regID before it can push to this device
                // here is where you might want to send it the regID for later use.
                console.log("regID = " + e.regID);
            }
            break;

            case 'message':
                // if this flag is set, this notification happened while we were in the foreground.
                // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                if (e.foreground)
                {
                    $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

                    // if the notification contains a soundname, play it.
                    var my_media = new Media("/android_asset/www/"+e.soundname);
                    my_media.play();
                }
                else
                {   // otherwise we were launched because the user touched a notification in the notification tray.
                    if (e.coldstart)
                        $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                    else
                    $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                }

                $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
                $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
            break;

            case 'error':
                $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
            break;

            default:
                $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
            break;
        }
    }

    function tokenHandler (result) {
        $("#app-status-ul").append('<li>token: '+ result +'</li>');
        // Your iOS push server needs to know the token before it can push to this device
        // here is where you might want to send it the token for later use.
    }

    function successHandler (result) {
        $("#app-status-ul").append('<li>success:'+ result +'</li>');
    }

    function errorHandler (error) {
        $("#app-status-ul").append('<li>error:'+ error +'</li>');
    }

    document.addEventListener('deviceready', onDeviceReady, true);

 </script>

解决方案

You can easily accomplish this,

Step 1 : Open your GCMIntentService.java file

Check for the method onMessage

in that method you will find from here they are passing the intent to your MyPhoneGapActivity.java ...Now what you have to do is you pass some extra information.... Passing some extra information to the intent

Check for this line

Intent notificationIntent = new Intent(context, MyPhoneGapActivity.class);
//here you pass the information
notificationIntent.putExtra ("url",url);

Can you see above i have passed extra information url ...for the complete code see below

  protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);

    // Extract the payload from the message
    Bundle extras = intent.getExtras();
    if (extras != null)
    {
        boolean foreground = this.isInForeground();

        extras.putBoolean("foreground", foreground);

        if (foreground){
            PushHandlerActivity.sendToApp(extras);
        }else{
            String message = extras.getString("message");
            String title = extras.getString("title");
            Notification notif = new Notification(android.R.drawable.btn_star_big_on, message, System.currentTimeMillis() );
            notif.flags = Notification.FLAG_AUTO_CANCEL;
            notif.defaults |= Notification.DEFAULT_SOUND;
            notif.defaults |= Notification.DEFAULT_VIBRATE;

            String url = "notify";

            Intent notificationIntent = new Intent(context, MyPhoneGapActivity.class);
            //here you pass the information
            notificationIntent.putExtra ("url",url);
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

            notif.setLatestEventInfo(context, title, message, contentIntent);
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
            mNotificationManager.notify(1, notif);
        }
    }
  }

Step 2:

In your MyPhoneGapActivity.java

package com.test;

import org.apache.cordova.DroidGap;

import android.os.Bundle;

public class MyPhoneGapActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setIntegerProperty("splashscreen", R.drawable.splash);
        super.setIntegerProperty("loadUrlTimeoutValue", 60000); 

        Bundle extras = getIntent().getExtras();
        String message = extras.getString("url");
        if(message == "notify"){
            super.loadUrl("file:///android_asset/www/home.html", 10000);
        }else{
            super.loadUrl("file:///android_asset/www/index.html", 10000);
        }
    }
}

Thats it now when you click the notification it will load the home.html file:-)