我有一个移动应用通过firebase云消息传递来自服务器的通知。
当我启动移动应用并进行订阅时,移动应用会收到服务器的通知。
问题是服务器何时停机并重新启动。重置后,服务器正在向移动应用程序发送通知(使用与之前相同的过程),但移动应用程序不会收到任何内容。请记住,移动应用程序保持不变(已注册)(服务器非常快速地执行重置)。
服务器的IP和端口保持不变......
以下是订阅正确频道的移动代码:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String NEW_CONTACTS_TOPIC = "new_contacts";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW));
}
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d(TAG, "Key: " + key + " Value: " + value);
}
}
Button subscribeButton = findViewById(R.id.subscribeButton);
subscribeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseMessaging.getInstance().subscribeToTopic(NEW_CONTACTS_TOPIC);
// Log and toast
String msg = getString(R.string.msg_subscribed);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
Button logTokenButton = findViewById(R.id.logTokenButton);
logTokenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get token
String token = FirebaseInstanceId.getInstance().getToken();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
try {
handleNow(remoteMessage);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (InterruptedException e) {
Log.e(TAG, e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
}
以下是我的服务器中发送通知的程序:
@Service
public class AndroidPushNotificationsService {
private static final String FIREBASE_SERVER_KEY = "XXXX";
private static final String FIREBASE_API_URL = "https://fcm.googleapis.com/fcm/send";
@Async
public CompletableFuture<String> send(HttpEntity<String> entity) {
RestTemplate restTemplate = new RestTemplate();
ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
restTemplate.setInterceptors(interceptors);
String firebaseResponse = restTemplate.postForObject(FIREBASE_API_URL, entity, String.class);
return CompletableFuture.completedFuture(firebaseResponse);
}
重要:当服务器启动时,服务器和移动应用之间的通信可以长时间工作......
如何解决此问题?
答案 0 :(得分:1)
在某些情况下,FCM可能无法发送消息。当消息太多(&gt; 100)
时会发生这种情况在Firebase文档中说: FCM服务器删除待处理邮件时调用 onDeletedMessages()。
这可能是由于:
答案 1 :(得分:0)
我找到了解决方案。
问题发生在我的服务器上:我发送了to: topic_name
而不是to: token_from_Server
,其中token_from_server
需要从移动应用程序(通过邮寄请求)发送到服务器,一旦应用程序正在播出,服务可用。
此外,移动应用程序需要向服务器发送新令牌,如果最后一个将被更改(虽然它从未发生在我身上,我已将此功能添加到我的移动应用程序和服务器映射中)
:)