我的应用程序关闭后如何收到推送通知?

时间:2017-07-06 14:08:21

标签: firebase push-notification xamarin.android firebase-cloud-messaging

这是Messaging服务类

[Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    class MyFirebaseMessagingService : FirebaseMessagingService
    {
        public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);
            SendNotification(message.GetNotification().Body);


    }

    private void SendNotification(string body)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetSmallIcon(Resource.Drawable.Notification)
            .SetContentTitle("")
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetSound(defaultSoundUri)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }

这是firebase id类

 [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    class MyFirebaseIdService : FirebaseInstanceIdService
    {

    public override void OnTokenRefresh()
    {
    base.OnTokenRefresh();
    Android.Util.Log.Debug("Refreshed Token:", 
    FirebaseInstanceId.Instance.Token);
    }

这是主要活动

Task.Run(() => {
var instanceId = FirebaseInstanceId.Instance;
instanceId.DeleteInstanceId();
Android.Util.Log.Debug("TAG", "{0} {1}", instanceId.Token, 
instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId), 
Firebase.Messaging.FirebaseMessaging.InstanceIdScope));


});

0 个答案:

没有答案