2017-12-27 142 views
2

的Android火力地堡背景通知我开发一个Android应用程序接收来自火力地堡推送通知。没有发射

我可以得到令牌并从邮差发送推送通知没有任何问题。

如果应用程序在前台,一切都按预期工作,并且我在onMessageReceived(我用各种有效载荷进行测试)中收到有效载荷。

但如果我关闭应用程序也没有收到什么。我尝试了很多有效载荷,和我读(有效载荷数据和通知之间的性差异)中的所有文档。

这里是我的项目使用我的班:

1 - 这是负责获得令牌

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

private static String TAG = "Android Push App"; 

@Override 
public void onTokenRefresh() { 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    sendRegistrationToServer(refreshedToken); 
} 

private void sendRegistrationToServer(String token) { 
    Log.d(TAG, "Did obtained token"); 
    Log.d(TAG, token); 
} 

3类 - 的扩展FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "Android Push App"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    sendNotification("Received notification"); 
} 

private void sendNotification(String messageBody) { 
    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_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.push_icon) 
        .setContentTitle("FCM Message") 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

2类 - 我的清单:

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <action android:name="FIREBASE_ACTIVITY" /> 

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

    <service android:name=".push.core.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 

    <service 
     android:name=".push.core.MyFirebaseMessagingService" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/push_icon" /> 

    <meta-data android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" /> 

</application> 

4 - 在MainActivity

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (getIntent().getExtras() != null) { 
     for (String key : getIntent().getExtras().keySet()) { 
      Object value = getIntent().getExtras().get(key); 
      Log.d("MainActivity", "Key: " + key + " Value: " + value); 
     } 
    } 
} 

5 - 最后,我想这有效载荷在邮差

POST https://fcm.googleapis.com/fcm/send 内容类型的应用程序/ JSON 授权密钥= AIzaSy(...)KY

JSON体(I试过示例):

{ 
"to": "dxe0RDKbP...m9Uc","notification" : { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
}} 

{ 
"to": "dxe0RDKbP...m9Uc","notification" : { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
}} 

和:

{ 
"to": "d3j-9OJ6R...C6w", 
"notification" : { 
    "title": "title", 
    "body": "body" 
}, 
"data": { 
    "tipo": "normal" 
}} 

还增加了 “优先” 键,这是行不通的。

我做错了什么?

感谢所有帮助,您可以给我:)

UPDATE

现在,它的工作。

FireBaseMessagingService与正在运行的Geofence Push(由App启动)之间存在冲突。

删除该地理栅栏服务一切正常后。

还使用通知数据按键的有效载荷中。

感谢

回答

1

试试这个

你不能把JSON关键'notification'在您的请求火力API,而是使用'data'

使用本

{ 
    "to": "dxe0RDKbP...m9Uc", 
    "data": { 
    "body" : "great match!", 
    "title" : "Portugal vs. Denmark", 
    "icon" : "push_icon", 
    "sound" : "default" 
    } 
} 

编辑 你可以只用身体和标题尝试

{ 
    "to": "dxe0RDKbP...m9Uc", 
    "data": { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark" 
    } 
} 

编辑新建

清单档案中的android:stopWithTask="false"服务属性添加这一点。

<service 
     android:name="com.yourapp.YourPushService" 
     android:stopWithTask="false" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
</service> 
+0

Stil不工作。我曾尝试过这种有效载荷。可以是关于图标或声音的东西吗? –

+0

@MichaelChaves请检查我的编辑答案。并告诉我这是什么结果。 –

+0

如果应用程序处于前景或背景中,它可以正常工作。如果我杀应用程序没有任何反应 –

0

您需要从json中删除通知负载并向其添加数据负载。这是因为Android已经采取的通知护理的内置功能,当它看到通知的有效载荷,即其每当通知有效载荷发送机器人直接将其发送到系统板车和onMessageReceived功能,不叫。

0

具体根据火力文档,火力地堡通知行为不同根据接收的应用程序的前景/背景状态。

onMessageReceived提供了大多数的消息类型,但下列情况除外:当你的应用程序在后台交付

通知消息。在这种情况下,通知将传递到设备的系统托盘。用户点击通知会默认打开应用程序启动器。

消息使用二者通知和数据有效载荷,无论背景和前景。在这种情况下,通知将传递到设备的系统托盘,并且数据有效负载将以您启动程序活动的目的附加内容传递。

所以,你需要从你的JSON删除通知的有效载荷,只需要保存的数据有效载荷来触发onMessageReceived当应用程序是在后台。

链接:https://firebase.google.com/docs/cloud-messaging/android/receive