在fcm

时间:2016-07-19 06:46:24

标签: android firebase-cloud-messaging

先生,我正试图从我的Android手机发送上游消息但未能这样做。不知道我错在哪里。 这是我的代码:

public class MainActivity extends AppCompatActivity {
Button button;
private AtomicInteger msgId;
FirebaseMessaging fm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FirebaseMessaging.getInstance().subscribeToTopic("hello");
    FirebaseInstanceId.getInstance().getToken();
    msgId = new AtomicInteger();
    button = (Button) findViewById(R.id.click);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),""+msgId,Toast.LENGTH_SHORT).show();
            fm = FirebaseMessaging.getInstance();
            RemoteMessage message=new RemoteMessage.Builder("<my sender id>@gcm.googleapis.com")
                    .setMessageId(Integer.toString(msgId.incrementAndGet()))
                    .addData("my_message", "Hello World")
                    .addData("my_action", "SAY_HELLO")
                    .build();
            fm.send(message);

        }
    });



 }
}

我根据文档实现了onMessageSent()和onSendError(),但这些方法从未被调用过。这是我的消息服务类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("test")
            .setContentText(remoteMessage.getData().get("message"));


    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());

}

@Override
public void onMessageSent(String s) {
    super.onMessageSent(s);
    Log.d("fcm", "onMessageSent: message sent");
    Toast.makeText(getApplicationContext(), "message:" + s, Toast.LENGTH_SHORT).show();

}

@Override
public void onSendError(String s, Exception e) {
    super.onSendError(s, e);
    Log.d("fcm", "onSendError: erroe");
    Toast.makeText(getApplicationContext(), "error:" + s, Toast.LENGTH_SHORT).show();
}
}

我从1周开始尝试这个仍然不知道我哪里错了。在logcat.please帮助中也没有。

4 个答案:

答案 0 :(得分:2)

您必须在邮件中设置TTL,例如

   RemoteMessage message=new RemoteMessage.Builder("<my sender id>@gcm.googleapis.com")
                .setMessageId(Integer.toString(msgId.incrementAndGet()))
                .addData("my_message", "Hello World")
                .addData("my_action", "SAY_HELLO")
                .setTtl(86400)
                .build();
  

setTtl(86400)这一行很重要,希望它会有所帮助。

答案 1 :(得分:1)

为了优化设备资源(尤其是电池),对onMessageSent和onSendError的回调进行了批处理,因此在您发送大约10个左右的上游消息之前,您可能不会收到回调。

有关详情,请参阅docs

答案 2 :(得分:0)

您需要让运行某些xmpp服务器的应用服务器与Firebase Messaging服务器通信。

答案 3 :(得分:0)

务必在邮件上设置生存时间。例如,new RemoteMessage.Builder(...).setTtl(<time in sec>)。我没有在FirebaseMessagingService和/或Cloud Connection Server(CCS)引用中记录此行为。