我正在研究Firebase Cloud消息传递服务 我一直关注文档现场,但是仍然遇到我从未遇到过的奇怪问题。问题是当我从控制台推送消息时,并不是一直都在调用OnMessageReceived()函数。要使其正常工作,我需要使缓存无效并重新启动IDE,然后在模拟器中重新安装该应用程序。我不确定为什么会首先发生这种情况。 Firebase助手还显示一切就绪。我将代码放在下面。
非常感谢。
package com.danaraddi.amazonsnsexample;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
System.out.println("Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
}
}
package com.danaraddi.amazonsnsexample;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//if the message contains data payload
//It is a map of custom keyvalues
//we can read it easily
if (remoteMessage.getData().size() > 0) {
//handle the data message here
}
//getting the title and the body
// String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
//then here we can use the title and body to build a notification
System.out.println("\n \n \n \n \n ************** Message From FireBaSE ************* \n \n " + body);
//
// Toast.makeText(this, "Message Received", Toast.LENGTH_SHORT).show();
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
System.out.println("Messages Deleted");
}
}
package com.danaraddi.amazonsnsexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("\n\n\n\n\n\n" + FirebaseInstanceId.getInstance().getToken().toString()+ "\n\n\n\n\n");
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.danaraddi.amazonsnsexample"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.google.firebase:firebase-messaging:15.0.2'
implementation 'com.google.firebase:firebase-core:15.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:4.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.danaraddi.amazonsnsexample">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
07-19 17:45:39.215 6105-6125/com.danaraddi.amazonsnsexample D/FA: Debug-level message logging enabled
07-19 17:45:39.272 6105-6105/com.danaraddi.amazonsnsexample I/System.out: eO-_t8px37Q:APA91bE0wZTbCLb1Kq0-lJ5vNSDFsFtBIXxFTJdXAQK7eJCU7ObzK3bTb8xVpJXrmC_GNw9BVhYRCxqrmWMNSbatgz7GMkvpofldTopCYW0_8LrO3jtDXN7c0k23tW9LfRWSPJNM5torC19uOT2Zm-JLwvK78wEvDw
07-19 17:45:39.285 6105-6128/com.danaraddi.amazonsnsexample D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-19 17:45:39.297 6105-6125/com.danaraddi.amazonsnsexample V/FA: Connecting to remote service
07-19 17:45:39.298 6105-6105/com.danaraddi.amazonsnsexample D/Atlas: Validating map...
07-19 17:45:39.330 6105-6125/com.danaraddi.amazonsnsexample V/FA: Connection attempt already in progress
07-19 17:45:39.333 6105-6125/com.danaraddi.amazonsnsexample I/FA: Tag Manager is not found and thus will not be used
07-19 17:45:39.339 6105-6125/com.danaraddi.amazonsnsexample D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=5535374082724063558}]
07-19 17:45:39.371 6105-6128/com.danaraddi.amazonsnsexample I/OpenGLRenderer: Initialized EGL, version 1.4
07-19 17:45:39.373 6105-6128/com.danaraddi.amazonsnsexample W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-19 17:45:39.374 6105-6125/com.danaraddi.amazonsnsexample V/FA: Connection attempt already in progress
07-19 17:45:39.376 6105-6125/com.danaraddi.amazonsnsexample V/FA: Connection attempt already in progress
07-19 17:45:39.382 6105-6128/com.danaraddi.amazonsnsexample D/EGL_emulation: eglCreateContext: 0xae8e22e0: maj 2 min 0 rcv 2
07-19 17:45:39.383 6105-6125/com.danaraddi.amazonsnsexample V/FA: Activity resumed, time: 6221785
07-19 17:45:39.398 6105-6128/com.danaraddi.amazonsnsexample D/EGL_emulation: eglMakeCurrent: 0xae8e22e0: ver 2 0
07-19 17:45:39.406 6105-6128/com.danaraddi.amazonsnsexample D/OpenGLRenderer: Enabling debug mode 0
07-19 17:45:39.417 6105-6128/com.danaraddi.amazonsnsexample D/EGL_emulation: eglMakeCurrent: 0xae8e22e0: ver 2 0
07-19 17:45:39.549 6105-6125/com.danaraddi.amazonsnsexample D/FA: Connected to remote service
07-19 17:45:39.549 6105-6125/com.danaraddi.amazonsnsexample V/FA: Processing queued up service tasks: 4
07-19 17:45:44.629 6105-6125/com.danaraddi.amazonsnsexample V/FA: Inactivity, disconnecting from the service
07-19 17:45:49.006 6105-6125/com.danaraddi.amazonsnsexample V/FA: Session started, time: 6231895
07-19 17:45:49.013 6105-6125/com.danaraddi.amazonsnsexample D/FA: Logging event (FE): session_start(_s), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=5535374082724063558}]
07-19 17:45:49.026 6105-6125/com.danaraddi.amazonsnsexample V/FA: Connecting to remote service
07-19 17:45:49.029 6105-6125/com.danaraddi.amazonsnsexample D/FA: Connected to remote service
07-19 17:45:49.030 6105-6125/com.danaraddi.amazonsnsexample V/FA: Processing queued up service tasks: 1
07-19 17:45:54.037 6105-6125/com.danaraddi.amazonsnsexample V/FA: Inactivity, disconnecting from the service
注意:我也关注了此文档 https://firebase.google.com/docs/cloud-messaging/android/receive?authuser=0
使用较新的firebase版本,方法是删除所有已弃用的函数,例如onTokenRefresh()等。此行为仍然相同