Xamarin服务通知不会开始

时间:2017-08-05 04:15:46

标签: c# android xamarin service

我有以下代码用于发送通知,但我在启动应用程序时看不到发送的通知。我错过了什么吗?

[Service]
public class MyService : Service
{

    const int NOTIFICATION_ID = 9000;

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        // Code omitted for clarity - here is where the service would do something.

        // Work has finished, now dispatch anotification to let the user know.
        Notification.Builder notificationBuilder = new Notification.Builder(this)
           // .SetSmallIcon(Resource.Drawable.ic_notification_small_icon)
           // .SetContentTitle(Resources.GetString(Resource.String.notification_content_title))
           .SetContentTitle("Comic Pull App")
            //.SetContentText(Resources.GetString(Resource.String.notification_content_text));
            .SetContentText("New Comics came out this week. Repull this month if needed.");
        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.Notify(NOTIFICATION_ID, notificationBuilder.Build());

        return StartCommandResult.Sticky;
    }

我在MainActivity

中有以下内容
StartService(new Intent(this, typeof(MyService)));

我在清单文件中有以下内容:

<application android:label="ToolbarFun" android:theme="@style/MyTheme">
  <service android:name=".MyService" android:exported="false"  android:enabled="true"/>
</application>

1 个答案:

答案 0 :(得分:0)

  

但是当我启动我的应用程序时,我没有看到通知。我错过了什么吗?

如果不使用SetSmallIcon()方法,则无法执行此操作。您必须为Notification设置一个图标,然后才能发送Notification