0

我想在使用c#輸入數據db時發送通知。我做了一些事,但我不知道我做錯了什麼在我的代碼,但問題是C#推送通知在仿真器上工作,但不在實際設備上

,當我在模擬器推送通知運行工作正常,但是當我在真機上運行 我沒有推通知。

請幫我一把。 在此先感謝。

這裏是我的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; 
        } 
       } 
      } 
     } 
    } 
+0

您是否肯定您正在使用該設備的相應註冊令牌?您是否收到錯誤回覆? –

回答

0

每當得到這個問題,然後只是重新啓動您的設備,它會工作得很好。

相關問題