Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#推送通知在模拟器上工作,但在实际设备上不工作_C#_Android_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

C#推送通知在模拟器上工作,但在实际设备上不工作

C#推送通知在模拟器上工作,但在实际设备上不工作,c#,android,firebase,push-notification,firebase-cloud-messaging,C#,Android,Firebase,Push Notification,Firebase Cloud Messaging,我想在数据使用c#输入db时发送通知。我做了一些事情,但我不知道我在代码中做错了什么,问题是 当我在emulator上运行时,推送通知工作正常,但当我运行 在真实设备上,我没有收到推送通知 请帮帮我。 提前谢谢 这是我的清单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.studen

我想在数据使用c#输入
db
时发送通知。我做了一些事情,但我不知道我在代码中做错了什么,问题是

当我在emulator上运行时,推送通知工作正常,但当我运行 在真实设备上,我没有收到推送通知

请帮帮我。 提前谢谢

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.studentcares.spps">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme">
    <receiver android:name="com.studentcares.spps.otpReader.SmsReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
    <service           
   android:name="com.studentcares.spps.firebase.MyFirebaseMesagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service android:name="com.studentcares.spps.firebase.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>  
    <activity android:name="com.studentcares.spps.dialogBox.NotifyNetworkConnectionMessage"/>
    <activity android:name="com.studentcares.spps.MainActivity"/>
    <activity android:name="com.studentcares.spps.SplashScreen"/>        
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

<uses-permission-sdk-23 android:name="android.permission.READ_SMS" />
<uses-permission-sdk-23 android:name="android.permission.RECEIVE_SMS" />
</manifest>
这是我的密码


无论何时出现此问题,只要重新启动您的设备,它就会正常工作。

您确定您正在为设备使用相应的注册令牌吗?您是否收到错误响应?
public class MyFirebaseMesagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    showNotification(remoteMessage);
    //showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(RemoteMessage remoteMessage){
    Intent intent = new Intent(this, MainActivity.class);
    //intent.putExtra("Message", remoteMessage.getNotification().getBody());
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setContentTitle("FCM Notification");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher_short);
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher));
    notificationBuilder.setPriority(2);
    notificationBuilder.setContentIntent(pendingIntent) ;
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,notificationBuilder.build());
}
}
try
    {

        string applicationID = "server api key";

        string senderId = "sender id";

        string deviceId = "registration fcm id";

        WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";
        var data = new
        {
            to = deviceId,
            notification = new
            {
                body = "test",
                title = "Push Notification",
                sound = "Enabled"

            }
        };
        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(data);
        Byte[] byteArray = Encoding.UTF8.GetBytes(json);
        tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
        tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
        tRequest.ContentLength = byteArray.Length;
        using (Stream dataStream = tRequest.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
            using (WebResponse tResponse = tRequest.GetResponse())
            {
                using (Stream dataStreamResponse = tResponse.GetResponseStream())
                {
                    using (StreamReader tReader = new StreamReader(dataStreamResponse))
                    {
                        String sResponseFromServer = tReader.ReadToEnd();
                        string str = sResponseFromServer;
                    }
                }
            }
        }
    }