2016-07-08 288 views
6

我在Android应用中实施了推送通知的Firebase。我实施了两项服务来注册令牌并在检测到通知时创建通知。当我的应用程序启动时它正在工作,但是当我的应用程序关闭时它不起作用。Firebase推送通知

public class FirebaseinstanceIdService extends FirebaseInstanceIdService { 

    @Override 
    public void onTokenRefresh() { 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.e("Firebase", refreshedToken); 
     MainActivity.setFirebaseToken(refreshedToken); 
    } 

} 

public class MyFirebaseMessageService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     //Displaying data in log 
     //It is optional 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 
     Log.e(TAG, "Notification Message Body: " + remoteMessage.getData().get("title")); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getData()); 
    } 

    //This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(Map<String, String> notification) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(notification.get("title")) 
      .setContentText(notification.get("body")) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
} 

我的清单:

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

,我有所求:

{ 
    "to": "ex1CtVz5bbE:APA91bHiM_BCun62A9iCobF1yV26Dwn-hYCDhkYPoWEG5ZdqH0P28UsE5q1v7QibwAX7AJ290-9en9L6f548_2b7WEbJ8RPEWlIotLczjjCP7xEOWqeJk6Iz44vilWYvdu4chPwfsvXD", 
    "data": { 
     "title": "Notification", 
     "body": "Message", 
    } 
} 

我已经发现的StackOverflow的几个解决方案,但它并不在我的情况下工作。我有一个API其余API调用API请求帖子:https://fcm.googleapis.com/fcm/send

所以,我的问题是:当应用程序关闭时Firebase是否处理推送通知以及如何?

谢谢你的答案

+0

你还可以指定你尝试过的* StackOverflow *解决方案吗?这将有助于缩小造成这种情况的因素。 –

+0

在api调用中,参数可以是通知对象或数据对象,我尝试过两种方法,但它不起作用。 – yozzy

+0

添加您的manifest.xml – Saini

回答

5

FCM不支持通知时应用程序被关闭。你的代码似乎没问题,所以我认为电池省电器(或节电器)可以杀死你的通知。我在华硕Zenfone上遇到了这样的问题,在使用华为和小米的情况下也有报道。只要禁用它们或在异常列表中添加您的应用程序(如果有的话)。在最近的Android版本中也有一种新的省电模式,尝试禁用它。

+0

好的,我使用华为。谢谢你的回答 – yozzy

+0

@yozzy,它解决了你的问题吗? – inthy

+0

谢谢我只是在HTC上进行测试,效果很好。你有想法解决手机上的这个问题不兼容吗? – yozzy