11

我正在检查Firebase Cloud Messaging以发送通知。当应用程序处于打开状态时,它已经实现它并接收通知。但是,如果我关闭应用程序,它不再发出通知。有没有解决方案。Android应用程序在关闭时未收到FCM通知

代码:

WebRequest wRequest; 
wRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 
wRequest.Method = "post"; 
wRequest.ContentType = " application/json;charset=UTF-8"; 
wRequest.Headers.Add(string.Format("Authorization: key={0}", AppId)); 

wRequest.Headers.Add(string.Format("Sender: id={0}", SenderId)); 

string postData = "{\"registration_ids\":[\"" + regIds + "\"], \"data\": "+ value +"}"; 

Byte[] bytes = Encoding.UTF8.GetBytes(postData); 
wRequest.ContentLength = bytes.Length; 

Stream stream = wRequest.GetRequestStream(); 
stream.Write(bytes, 0, bytes.Length); 
stream.Close(); 

WebResponse wResponse = wRequest.GetResponse(); 

消息服务 -

public class MessagingService extends FirebaseMessagingService { 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData(); 
     sendNotification(data); 
    } 

    public void showMessage(Map<String, String> serverData) { 
     Intent i = new Intent(this,MainActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setAutoCancel(true) 
       .setContentTitle(serverData.get("Title")) 
       .setContentText(serverData.get("Message")) 
       .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
       .setContentIntent(pendingIntent); 

     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     manager.notify(Integer.parseInt(serverData.get("ItemId")),builder.build()); 
    } 

    private void sendNotification(Map<String, String> serverData) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

     long[] pattern = {500,500,500,500,500}; 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
       .setContentTitle(serverData.get("Title")) 
       .setContentText(serverData.get("Message")) 
       .setAutoCancel(true) 
       .setVibrate(pattern) 
       .setLights(Color.BLUE,1,1) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(Integer.parseInt(serverData.get("ItemId")), notificationBuilder.build()); 
    } 

} 

主要活动 -

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     FirebaseMessaging.getInstance().subscribeToTopic("test"); 
     FirebaseInstanceId.getInstance().getToken(); 
    } 
} 

Manifest-

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="test.com.firebasenotify"> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service android:name=".MessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 
     <service android:name=".InstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

    </application> 

</manifest> 
+1

显示FCM监听器类,并显示POST请求您对FCM –

+0

更新消息服务,FCM职位和主要活动代码 –

+0

你在的onReceive收到消息至少? –

回答

8

代码没有问题。我正在使用Mi note 4,并且在应用程序关闭时它不会在Mi4中显示通知。我测试了与其他Android设备和它的工作正常。

感谢Tim Castelijns和Frank van Puffelen参与对话。

+2

对于MI,您需要将应用程序置于安全模式下的自动启动模式才能在应用程序关闭时收到通知 –

+0

感谢man,redmi prime2也得到同样的问题。您是否在MI设备中发现了此问题的任何解决方案? – sandeepmaaram

-1

添加在POST有效载荷将解决这一参数的这个problem.The值必须是从0到2419200秒的持续时间time_to_live键,它对应于用于该FCM存储并尝试传递的最大时间周期消息。 “time_to_live”:3Refer Firebase Docs

+1

只有当用户回到应用程序时,这可能是有效的。如果您看到代码中没有通知对象,则为 – ioanb7

-3

作为每FCM文档onMessageReceived()时的应用程序是在背景将不被调用。你应该在发送顺序通知对象,以显示它在系统托盘中,当用户点击它发射活动将开放数据处理为额外意图。对于有效载荷,您应该使用数据对象。 see docsReceive Messages in an Android App

+2

。他只发送数据对象 –

+0

您还需要发送通知对象。 – Sjd

+2

但是如果我们将发送通知对象,那么它将不会触及onMessageReceived,并且通知将由android本身处理,并将显示在通知托盘(没有您的客户端应用程序)中。如果我们发送没有通知对象,但有数据对象,那么它总是会触发onMessageReceived。 –

相关问题