2
嗨,大家對我的糟糕英語感到抱歉。你好嗎?幾個月前,我實施了FCM推送通知。主要問題是應用程序關閉時,通知不會在設備上發生時發送。應用程序關閉時未收到Android FCM推送通知
我嘗試了很多thigs的作品;當通知發送服務器響應與「成功」,但我從來沒有收到。
這是完整的代碼。 Android,Manifiest和服務器。
安卓
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.accionait.canje365.R;
import com.accionait.canje365.activities.ChatActivity;
import com.accionait.canje365.activities.HomeActivity;
import com.accionait.canje365.constants.Constants;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage);
}
private void showNotification(RemoteMessage remoteMessage) {
Uri uriDefaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_notification)
.setLargeIcon((((BitmapDrawable)getResources().getDrawable(R.mipmap.ic_notification)).getBitmap()))
.setContentTitle("TITLE")
.setContentText("MESSAGE")
.setContentInfo("0")
.setAutoCancel(true)
.setTicker("Canje365")
.setSound(uriDefaultSound);
Intent home = new Intent(this, HomeActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0, home, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
服務器:
var message = {
to: token,
data: {valor: 'test'}
};
fcm.send(message, function(err, response){
if(err) {
console.log('Error: ' + err);
}
else {
console.log('Notificacion enviada: ', response);
}
});
Manifiest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.accionait.canje365">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name=".sync.RunIntentService" />
<service android:name=".sync.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".sync.FirebaseIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_API_KEY" />
<activity
android:name=".activities.MainActivity"
android:theme="@style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.HomeActivity"
android:label="@string/title_activity_login"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
這是所有球員。我希望你可以幫助我!我不知道該怎麼做。謝謝!
我們非常感謝您的問候。我猜你正在使用Firebase控制檯發送正確的通知?你必須實現你自己的服務器,我知道,額外的工作,通過相同的實現我的Java,你也可以使用PHP。閱讀此:http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – Manny265
和官員docs解釋消息的類型,當每個收到https:/ /firebase.google.com/docs/cloud-messaging/concept-options – Manny265
@ Manny264是我自己的服務器,不是Firebase控制檯。我閱讀關於消息類型的文檔。你可以看到我只發送數據消息來處理應用程序關閉時。 我已經試過這個。謝啦!如果您有其他建議,請告訴我。 再次感謝。 –