Vivek Patel
Vivek Patel

Reputation: 25

How to implement Local Push notification in Xamarin Android

I am having the Xamarin.Forms application. I want to implement the local push notification for Android platform, through which an application can receive the notification whenever the app is minimized or phone is locked. I also want to wake-up the phone and display the notification on the lock screen whenever the phone is locked.

As I am new in this kindly help me.

Thanks in advance.

Vivek

Upvotes: 0

Views: 4042

Answers (2)

Leo Zhu
Leo Zhu

Reputation: 15031

I think you should consider two things.

  1. Listen for the screen lock:

create a ScreenListener.cs :

public class ScreenListener
{
    private Context mContext;
    private ScreenBroadcastReceiver mScreenReceiver;
    private static ScreenStateListener mScreenStateListener;

    public ScreenListener(Context context)
    {
        mContext = context;
        mScreenReceiver = new ScreenBroadcastReceiver();
    }

    /**
     * screen BroadcastReceiver
     */
    private class ScreenBroadcastReceiver : BroadcastReceiver
    {
        private String action = null;

        public override void OnReceive(Context context, Intent intent)
        {
            action = intent.Action;
            if (Intent.ActionScreenOn == action)
            { // screen on
                mScreenStateListener.onScreenOn();
            }
            else if (Intent.ActionScreenOff == action)
            { // screen off
                mScreenStateListener.onScreenOff();
            }
            else if (Intent.ActionUserPresent == action)
            { // unlock
                mScreenStateListener.onUserPresent();
            }
        }
    }

/**
 * Begin to listen screen state
 *
 * @param listener
 */
public void begin(ScreenStateListener listener)
{
    mScreenStateListener = listener;
    registerListener();
    getScreenState();
}

/**
 * Get screen state
 */
private void getScreenState()
{
    PowerManager manager = (PowerManager)mContext
            .GetSystemService(Context.PowerService);
    if (manager.IsScreenOn)
    {
        if (mScreenStateListener != null)
        {
            mScreenStateListener.onScreenOn();
        }
    }
    else
    {
        if (mScreenStateListener != null)
        {
            mScreenStateListener.onScreenOff();
        }
    }
}

/**
 * stop listen screen state
 */
public void unregisterListener()
{
    mContext.UnregisterReceiver(mScreenReceiver);
}

/**
 * regist screen state broadcast
 */
private void registerListener()
{
    IntentFilter filter = new IntentFilter();
    filter.AddAction(Intent.ActionScreenOn);
    filter.AddAction(Intent.ActionScreenOff);
    filter.AddAction(Intent.ActionUserPresent);
    mContext.RegisterReceiver(mScreenReceiver, filter);
}

    public interface ScreenStateListener
    {
        // Returns screen status information to the caller
        void onScreenOn();

        void onScreenOff();

        void onUserPresent();
    }
}

then in the MainActivity.cs:

public class MainActivity : AppCompatActivity,ScreenStateListener
{
    ScreenListener mScreenListener;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        mScreenListener = new ScreenListener(this);
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        mScreenListener.unregisterListener();
    }

    protected override void OnResume()
    {
        base.OnResume();
        mScreenListener.begin(this);

    }
    public void onScreenOn()
    {
        Console.WriteLine("onScreenOn");
    }

    public void onScreenOff()
    {
        Console.WriteLine("onScreenOff");
    }

    public void onUserPresent()
    {
        Console.WriteLine("onUserPresent");
    }
}
  1. Create local Notification

finally you just push the notification in the onScreenOff() method.

Upvotes: 1

Woj
Woj

Reputation: 833

What you need is push notifications not local notifications. Local notifications only work if your app is running. Push notifications are handled by OS (Android in this case). Xamarin is recommending using Firebase service to handle it. Procedure is pretty straight forward all what you have to do is follow Tutorial from official Xamarin site.

Remote Notifications with Firebase Cloud Messaging

Upvotes: 0

Related Questions