如何在 Xamarin 中实现本地推送通知 Android

How to implement Local Push notification in Xamarin Android

我正在申请 Xamarin.Forms。我想为 Android 平台实现本地推送通知,应用程序可以通过它在应用程序最小化或 phone 被锁定时收到通知。我还想唤醒 phone 并在 phone 被锁定时在锁定屏幕上显示通知。

由于我是新手,请帮助我。

提前致谢。

维维克

您需要的是推送通知,而不是本地通知。本地通知仅在您的应用 运行 时有效。推送通知由 OS 处理(在本例中为 Android)。 Xamarin 建议使用 Firebase 服务来处理它。过程非常简单,您只需按照 Xamarin 官方网站上的教程进行操作即可。

Remote Notifications with Firebase Cloud Messaging

我认为你应该考虑两件事。

1.Listen为锁屏:

创建 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();
}
}

然后在 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");
    }


}

2.creat loacal Notification

最后你只需在 onScreenOff() 方法中推送通知。