在MyApp

时间:2017-10-27 16:26:11

标签: xamarin

我使用PHP脚本和下面的代码实现FCM推送通知,我想向所有使用名称(新闻)订阅主题的设备发送通知,但是在调用我的脚本时没有收到消息,我不知道出了什么问题

public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
    }
    public void SendNotificatios(string body, string Header)
    {
        Notification.Builder builder = new Notification.Builder(this);
        builder.SetSmallIcon(Resource.Drawable.Icon);
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
        builder.SetContentIntent(pendingIntent);
        builder.SetSmallIcon(Resource.Drawable.Icon);
        builder.SetContentTitle(Header);
        builder.SetContentText(body);
        builder.SetDefaults(NotificationDefaults.Sound);
        builder.SetAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.Notify(1, builder.Build());
    }
}

}

MainActivity

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        msgText = FindViewById<TextView>(Resource.Id.msgText);
        if (Intent.Extras != null)
        {
            foreach (var key in Intent.Extras.KeySet())
            {
                var value = Intent.Extras.GetString(key);
                Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
            }
        }
        IsPlayServicesAvailable();
        var logTokenButton = FindViewById<Button>(Resource.Id.logTokenButton);
        logTokenButton.Click += delegate
        {
            Log.Debug(TAG, "InstanceID token: " + FirebaseInstanceId.Instance.Token);
        };
        var subscribeButton = FindViewById<Button>(Resource.Id.subscribeButton);
        subscribeButton.Click += delegate
        {
            FirebaseMessaging.Instance.SubscribeToTopic("test");
            Log.Debug(TAG, "Subscribed to remote notifications");
        };
    }

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amgad.android" android:versionCode="1" android:versionName="1.0">  
<uses-sdk android:minSdkVersion="15" />  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
<uses-permission android:name="android.permission.WAKE_LOCK" />  
<application android:label="FCM">  
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />  
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:enabled="true" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">  
        <intent-filter>  
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />  
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />  
            <category android:name="${applicationId}" /> </intent-filter>  
    </receiver>  
</application>  

0 个答案:

没有答案