我剛剛添加聊天功能到我的應用程序後添加核心功能的應用程序。我開始實施Firebase推送通知到應用程序,並按照從官方文檔的所有步驟。因此,每當我從Firebase通知發送消息控制檯它表明,當應用程序在前臺但當應用在後臺它顯示logcat的這些線,但沒有通知Android後臺通知不工作在Firebase
09-27 16:11:37.645 19946-19946/com.example.com.appD/dalvikvm: DexOpt: couldn't find field Landroid/os/Message;.sendingUid
09-27 16:11:37.645 19946-19946/com.example.com.appW/dalvikvm: VFY: unable to resolve instance field 135
09-27 16:11:37.645 19946-19946/com.example.com.appD/dalvikvm: VFY: replacing opcode 0x52 at 0x0000
這裏是我的火力地堡實例ID服務類
public class FireBase_InstanceID_Service extends FirebaseInstanceIdService {
public static final String TAG="==Firebase ID===";
private static final String SubscribeTopic="Chat";
String refreshedToken;
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
refreshedToken= FirebaseInstanceId.getInstance().getToken();
Log.d(TAG,"Here Is Token "+refreshedToken);
FirebaseMessaging.getInstance().subscribeToTopic(SubscribeTopic);
}
private void sendRegistrationToServer(String Token){
//TODO: Send Token To APP server
}
}
這裏是我的火力地堡消息服務類
public class FireBase_Messaging_Service extends FirebaseMessagingService {
public static final String TAG="==FireBase MSG==";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG,"From "+remoteMessage.getFrom());
if (remoteMessage.getData().size()>0){
Log.d(TAG,"Message Data "+remoteMessage.getData());
}
if (remoteMessage.getNotification()!=null){
Log.d(TAG,"Message Notification Body "+remoteMessage.getNotification().getBody());
}
Log.d(TAG,"FCM Message ID "+remoteMessage.getMessageId());
Log.d(TAG,"FCM Notification Message: "+remoteMessage.getNotification());
Log.d(TAG,"FCM Data Message "+remoteMessage.getData());
showNotification(remoteMessage.getNotification().getBody());
}
private void showNotification(String Message){
Intent intent=new Intent(this, UserProfile.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,5,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder= (NotificationCompat.Builder) new NotificationCompat.Builder (this)
.setAutoCancel(true)
.setContentTitle("New Notification")
.setContentText(Message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_light_normal)
.setContentIntent(pendingIntent);
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(5,builder.build());
}
}
清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.com.app">
<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:theme="@style/AppTheme">
<activity
android:name=".Navigation_Drawer"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<service
android:name=".ImageDownloading"
android:enabled="true"
android:exported="false">
</service>
<!--FIREBASE SERVICES -->
<service android:name=".FireBase_Messaging_Service">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"></action>
</intent-filter>
</service>
<service android:name=".FireBase_InstanceID_Service">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
</intent-filter>
</service>
<!--FIREBASE SERVICES -->
</application>
</manifest>
所以對我來說,我無法獲得推通知托盤通知在後臺,或當應用程序被關閉有了這些條目logcat(我不明白)
分享您的清單文件 –
@AdnanAli請檢查更新的問題 – androidXP
檢查此鏈接。 http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase –