我正在開發使用火力地堡雲Messagging,有一個奇怪的問題通過FCM由遠程服務器發送一個推送通知的應用程序。火力地堡通知沒有在Android虛擬設備工作
奇怪行爲我說的是一切工作就像一個魅力,當我在USB連接手機Oneplus2(安卓6.0.1 API 23)運行我的應用程序。但是,在一個虛擬設備中運行(我試圖創建許多人,用不同的API版本和Android版本)似乎並不甚至要執行的onMessageReceived的功能FirebaseMessagingService。
我懷疑從AVD Manager創建的虛擬設備中的不正確的配置,但根據火力地堡的文件,我需要
- 運行Android 4.0(冰淇淋三明治)的設備或更新,和谷歌播放服務10.2.0或更高
- 的谷歌Play服務SDK從谷歌庫,可在Android SDK管理器
- Android Studio中,版本的最新版本1.5或更高
和我確定這三個必要條件。
任何幫助,將不勝感激。
編輯
FirebaseMessagingService.java
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
Log.i("PVL", "MESSAGE RECEIVED!!");
}
private void showNotification(String message) {
Intent i = new Intent(this, WalkersMain.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Walkers")
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ctscts.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/GdxTheme"
>
<activity
android:name="com.ctscts.game.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
注:我想無論是從火力地堡控制檯和我的遠程服務器發送請求,具有相同的行爲。
如何執行發送請求:
@RequestMapping(value = { "/send" }, method = RequestMethod.GET)
public ResponseEntity<Void> sentNotification(@RequestBody String body, UriComponentsBuilder ucBuilder) {
HttpHeaders headers = new HttpHeaders();
String serverKey = "AAAAfZXBxHk:APA91bFlZ6ty7DQbLZkF5LJJUQJbYlO6JuJ8JmcQetG_cyAYD69QvPT6KK3KWkfTWHEWfgrwg7C7GSS1rWlNcumNNmkWfVae5p89";
List<User> lista = userService.findAllUsers();
List<String> listaToken = new ArrayList<String>();
for (User user : lista) {
listaToken.add(user.getToken());
}
Sender sender = new FCMSender(serverKey);
Message message = new Message.Builder()
.collapseKey("message")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message", "Notification from Java application")
.build();
MulticastResult result;
try {
result = sender.send(message, listaToken, 1);
System.out.println("Result: " + result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ResponseEntity<Void>(headers, HttpStatus.NO_CONTENT);
}
在調試模式下,當我執行行結果= sender.send(消息,listaToken,1); 消息變量的值是
Message(collapseKey=message,
timeToLive=3,
delayWhileIdle=true,
data: {
message=Notification from Java application
}
)
和listaToken包含令牌到達的列表。 下面結果:
結果:
MulticastResult(multicast_id=9084949851503613851,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1489049633129649%6a8d477338eb0007 ]]
https://firebase.google.com/docs/test-lab/avds –
EHI @sanketpahuja,謝謝! :-) – Carlessa
任何建議? – Carlessa