android - firebase 云消息在某些设备中未收到通知
问题描述
我使用 FCM 推送通知,它在后台和前台模式下成功工作,在我用来调试的设备中,而另一个设备在后台模式下不工作。这是我的代码,我希望您能帮助您了解问题所在。是来自电话的问题,那么如何解决?或来自谷歌服务。这里 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);
解决方案
推荐阅读
- c++ - 如何将参数包转换为 std::tuple 以外的其他东西?
- excel - VBA:填写选择框后如何更新页面
- sql - Postgres 中的 Schema.Table
- android - 如何在 Java 中将视图绑定与包含的视图一起使用?
- excel - 如何阻止一组单元格在 Excel 的公式中递增
- python - python / pip - whl 不是这个平台上支持的轮子
- gradle - Gitlab Runner 配置以忽略在服务器上构建的文件夹
- tensorflow2.0 - `_UserObject` 对象没有属性 `call_and return_conditional_losses`
- python - Scrapy 输出 JSON 或 CSV
- python - 尝试使用经过验证的用户令牌调用 api 时出现值错误