我使用FCM推送通知,该通知在后台调试模式下成功地在我调试过的设备中运行,而另一台设备在后台模式下不工作。 这是我的代码,希望对您有帮助。了解电话的问题,如何解决?或来自Google服务。 这里MyFirebaseInstanceIDService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
SharedPrefManager sharedPrefManager;
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.wtf("myfirebase", "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
public void sendRegistrationToServer(String refreshedToken )
{
sharedPrefManager=new SharedPrefManager(getApplicationContext());
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<Result> call = apiInterface.insert_token(sharedPrefManager.getPrefVal("id_shared"),refreshedToken);
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
//Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
}
);
}
}
此处是MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
String TAG ="fbmessage";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
createNotification("Hello",remoteMessage.getData().get("body"));
}
public void createNotification(String title, String sub) {
// Prepare intent which is triggered if the
// notification is selected
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(sub).setSmallIcon(R.drawable.biglogored)
.setContentIntent(pendingIntent)
.build();
long[] vibrate = { 0, 100, 200, 300 };
noti.vibrate = vibrate;
NotificationManager notificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
这里是我在mainfist中设置的服务
<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>
此处是PHP代码。
$fields = array('to'=>$token,
'data'=>array('title'=>$title,'body'=> $Message));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_IPRESOLVE,CURL_IPRESOLV_V4);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);