【发布时间】:2017-07-20 09:48:32
【问题描述】:
我正在尝试使用FCM 发送UpStream Message,所以我按照谷歌上的教程进行操作。
如下MainActivity中的代码所示,当点击按钮时我发送Upstream message,然后在MyAndroidFirebaseMsgService中我应该看到一个Log消息,如图所示
下面在MyAndroidFirebaseMsgService。
但是发生的情况是,即使我多次按下按钮,MyAndroidFirebaseMsgService 和 onMessageSent 中的 Log 消息也不会显示。
仅当从FCM 向应用程序发送downstream message 时,才能显示MyAndroidFirebaseMsgService 中的Log 消息,在这种情况下,Logs 中的Logs
将显示MyAndroidFirebaseMsgService。
请告诉我为什么在发送UpStream message 后没有显示onMessageSent 中的日志消息?以及如何解决它。
主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnSendUpstreamMsg = (Button) findViewById(R.id.btn_send_upstream_message);
mBtnSendUpstreamMsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseMessaging fm = FirebaseMessaging.getInstance();
fm.send(new RemoteMessage.Builder("673xxxxx" + "@gcm.googleapis.com")
.setMessageId("2")
.addData("my_message", "Hello World")
.addData("my_action","SAY_HELLO")
.build());
}
});
}
MyAndroidFirebaseMsgService:
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private final static String TAG = MyAndroidFirebaseMsgService.class.getSimpleName();
@Override
public void onMessageSent(String s) {
super.onMessageSent(s);
Log.d(TAG, "onMessageSent: upstream message");
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "onMessageReceived: downstream message");
//Log data to Log Cat
Log.d(TAG, "onMessageReceived->From: " + remoteMessage.getFrom());
Log.d(TAG, "onMessageReceived->Notification Message Body: " + remoteMessage.getNotification().getBody());
//create notification
createNotification(remoteMessage.getNotification().getBody());
}
private void createNotification( String messageBody) {
Intent intent = new Intent( this , ResultActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Android Tutorial Point FCM Tutorial")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
【问题讨论】:
-
我引用的问题的答案和 cmets 表明,由于 Firebase 中的错误,
onMessageSent()不会被调用,除非RemoteMessage是使用setTtl()构建的。我没有在 11.0.2 中观察到这一点。请注意,在调用onMessageSent()之前有大约 20 分钟的延迟。这是explained in the docs:为了优化网络使用,FCM 会批量响应 onMessageSent 和 onSendError,因此每条消息的 ack 可能不是立即的 -
但是您提供的链接没有回答我的问题,因为在您提供的链接中我应该使用
RemoteMessage并且它没有在我的代码中定义..知道如何导入/使用@987654349 @类 -
我更新了我的评论。如果您使用的是 11.0.2 版,则不需要
setTtl()。您是否要等待 20 分钟才能呼叫onMessageSent()? -
我正在使用 10.0.1.... 编译 'com.google.firebase:firebase-messaging:10.0.1' 编译 'com.google.android.gms:play-services:10.0. 1'
标签: android firebase firebase-realtime-database push-notification firebase-cloud-messaging