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

Posted

技术标签:

【中文标题】C# 推送通知在模拟器上工作但在真实设备上不工作【英文标题】:C# push notification working on emulator but not on real device 【发布时间】:2017-05-06 07:41:28 【问题描述】:

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

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

请帮帮我。 提前致谢。

这是我的manifest

<?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>

这里是firebaseMessaging服务代码,

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());


这是我的 C# 代码,

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;
                    
                
            
        
    

【问题讨论】:

您确定您正在为设备使用相应的注册令牌吗?您是否收到错误响应? 【参考方案1】:

每当遇到此问题时,只需重新启动设备即可正常工作。

【讨论】:

以上是关于C# 推送通知在模拟器上工作但在真实设备上不工作的主要内容,如果未能解决你的问题,请参考以下文章

Web 推送通知在 Android 上工作,但在 IOS 上不工作

在 android 中关闭应用程序时,在真实设备上未收到一个信号通知

带有 p8 文件的 Apple 推送通知在我的 Mac 上工作,但在 Linux 服务器上不起作用

表格视图在真实设备上不刷新

将控制器推送到 UINavigationController 在 iPad 上工作,但在 iPhone 上不工作

Firebase setValue() 在模拟器上工作,但在设备上不工作,有人知道为啥吗? iOS 10,斯威夫特 3.1