Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 76418

ReceivedRemoteNotification method is not called.

$
0
0

I have coded for receiving notification. But it is not working presently.

code for calling remote notifications are as

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{

    public static UIFont fonttext = UIFont.FromName ("HelveticaNeue-Light", 14);
    public static UIColor color = UIColor.FromRGB (2, 20, 20);
    public static UIColor secondaryColor = UIColor.FromRGB (1, 101, 156);


    UIWindow window;
    //UINavigationController nav;
    string _deviceToken="";
    UINavigationController nav;
    //uibarbu
    PilatLogin _HomeViewController;


    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {

        this.window = new UIWindow (UIScreen.MainScreen.Bounds);
        this.nav = new UINavigationController ();
        nav.NavigationBar.BarStyle = UIBarStyle.Black;
        nav.NavigationBar.BarTintColor = UIColor.White;
        _HomeViewController= new PilatLogin ();


        this.window.RootViewController = nav;
        this.window.MakeKeyAndVisible ();

        //UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Alert| UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound);
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge| UIRemoteNotificationType.Sound;
        UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);

        //  UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationType);
        //  processNotification(options, true);
        //new UIAlertView ("Remote Notification","You have received notification from PiLAT",null,"OK",null).Show();
        nav.PushViewController(_HomeViewController, true);

        return true;
    }



    public override void RegisteredForRemoteNotifications(UIApplication application,NSData deviceToken)
    {

        var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

        var strFormat = new NSString("%@");
        var dt = new NSString(MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr(new MonoTouch.ObjCRuntime.Class("NSString").Handle, new MonoTouch.ObjCRuntime.Selector("stringWithFormat:").Handle, strFormat.Handle, deviceToken.Handle));
        var newDeviceToken = dt.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");

        if (string.IsNullOrEmpty(oldDeviceToken) || !deviceToken.Equals(newDeviceToken))
        {
            //TODO: Put your own logic here to notify your server that the device token has changed/been created!
        }


        NSUserDefaults.StandardUserDefaults.SetString(newDeviceToken, Constant.DeviceTokenId);
        NSUserDefaults.StandardUserDefaults.Synchronize ();
        Console.WriteLine("Device Token: " + newDeviceToken);

        string trimmeddeviceToken = deviceToken.Description;
        if (!string.IsNullOrEmpty (trimmeddeviceToken)) {

            trimmeddeviceToken = trimmeddeviceToken.Trim ('>');
            trimmeddeviceToken = trimmeddeviceToken.Trim ('<');
        }
        _deviceToken = trimmeddeviceToken;
        //new UIAlertView ("device token is    ",_deviceToken,null,"OK",null).Show();
        Constant.DeviceTokenId = newDeviceToken;
        //UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
        NSUserDefaults.StandardUserDefaults.SetString (_deviceToken, Constant.DeviceTokenId);
        NSUserDefaults.StandardUserDefaults.Synchronize ();




    }



    public override void FailedToRegisterForRemoteNotifications(UIApplication application,NSError error)
    {

        new UIAlertView ("FailedToRegisterForRemoteNotifications","FailedToRegisterForRemoteNotifications",null,"OK",null).Show();
        Console.WriteLine ("Error AppDelegate push notification");
        new UIAlertView ("Error For Push Notification",error.LocalizedDescription,null,"OK",null).Show();
    }



    public override void   ReceivedRemoteNotification(UIApplication application,NSDictionary userInfo)
    {

        //Debug.WriteLine(userInfo.ToString());
        NSObject inAppMessage;

        bool success = userInfo.TryGetValue(new NSString("inAppMessage"), out inAppMessage);

        if (success)
        {
            var alert = new UIAlertView("Got push notification", inAppMessage.ToString(), null, "OK", null);
            alert.Show();
        }



        //new UIAlertView ("ReceivedRemoteNotification11111111","found",null,"OK",null).Show();
        var strFormat = new NSString("%@");
        //processNotification(userInfo, false);
        var dt = new NSString(MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr(new    MonoTouch.ObjCRuntime.Class("NSString").Handle, new MonoTouch.ObjCRuntime.Selector("stringWithFormat:").Handle, strFormat.Handle, userInfo.Handle));
        var newnotificationid = dt.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
        UIApplication.SharedApplication.ApplicationIconBadgeNumber =0;
        //new UIAlertView ("ReceivedRemoteNotification",newnotificationid,null,"OK",null).Show();
        Console.WriteLine ("Recieve Notification calling ");
    }

    public override void ReceivedLocalNotification(UIApplication application, UILocalNotification localNotification)
    {
        new UIAlertView (localNotification.AlertAction, localNotification.AlertBody, null, "OK", null).Show ();
        UIApplication.SharedApplication.ApplicationIconBadgeNumber =0;
    }


    void processNotification(NSDictionary options, bool fromFinishedLaunching)
    {

        //Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
        if (null != options && options.ContainsKey(new NSString("aps")))
        {
            //Get the aps dictionary
            NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;

            string alert = string.Empty;
            string sound = string.Empty;
            int badge = -1;

            //Extract the alert text
            //NOTE: If you're using the simple alert by just specifying "  aps:{alert:"alert msg here"}  "
            //      this will work fine.  But if you're using a complex alert with Localization keys, etc., your "alert" object from the aps dictionary
            //      will be another NSDictionary... Basically the json gets dumped right into a NSDictionary, so keep that in mind
            if (aps.ContainsKey(new NSString("alert")))
                alert = (aps[new NSString("alert")] as NSString).ToString();

            //Extract the sound string
            if (aps.ContainsKey(new NSString("sound")))
                sound = (aps[new NSString("sound")] as NSString).ToString();

            //Extract the badge
            if (aps.ContainsKey(new NSString("badge")))
            {
                string badgeStr = (aps[new NSString("badge")] as NSObject).ToString();
                int.TryParse(badgeStr, out badge);
            }

            //If this came from the ReceivedRemoteNotification while the app was running,
            // we of course need to manually process things like the sound, badge, and alert.
            if (!fromFinishedLaunching)
            {
                //Manually set the badge in case this came from a remote notification sent while the app was open
                if (badge >= 0)
                    UIApplication.SharedApplication.ApplicationIconBadgeNumber = badge;
                //Manually play the sound
                if (!string.IsNullOrEmpty(sound))
                {
                    //This assumes that in your json payload you sent the sound filename (like sound.caf)
                    // and that you've included it in your project directory as a Content Build type.
                    var soundObj = MonoTouch.AudioToolbox.SystemSound.FromFile(sound);
                    soundObj.PlaySystemSound();
                }

                //Manually show an alert
                if (!string.IsNullOrEmpty(alert))
                {
                    UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);
                    avAlert.Show();
                }
            }
        }

        //You can also get the custom key/value pairs you may have sent in your aps (outside of the aps payload in the json)
        // This could be something like the ID of a new message that a user has seen, so you'd find the ID here and then skip displaying
        // the usual screen that shows up when the app is started, and go right to viewing the message, or something like that.
        //if (null != options && options.ContainsKey(new NSString("customKeyHere")))
        //{
        //  launchWithCustomKeyValue = (options[new NSString("customKeyHere")] as NSString).ToString();

            //You could do something with your customData that was passed in here
        //}
    }

}

Please help me in shorting the issue.


Viewing all articles
Browse latest Browse all 76418

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>