我正在尝试使用FCM,并且通知仅在不使用应用程序时有效。
当我从设备A向设备B发送通知时,设备B收到消息并“以默认声音显示通知弹出消息”,并且一切正常……(这发生在设备B不使用应用程序时)。
当我从设备A向设备B发送通知时,设备B会在onMessageReceived()方法中接收到消息,但“不显示带有默认声音的通知弹出消息”。(这发生在设备B使用应用程序,我的意思是打开并正在使用该应用程序时。)
这是我的代码 FireIDService.java
public class FireIDService extends FirebaseInstanceIdService {
private final String TAG = "FireIDService";
@Override
public void onTokenRefresh() {
String tkn = FirebaseInstanceId.getInstance().getToken();
Log.d("Not","Token ["+tkn+"]");
sendRegistrationToServer(tkn);
}
private void sendRegistrationToServer(String token) {
saveDeviceToken(token);
}
private void saveDeviceToken(String deviceToken) {
//some code..
if(response.body().getStatus() == 1){
doStuff();
}
//some code...
}
@Override
public void onFailure(Call<SaveDeviceTokenResponse> call, Throwable t) {
//code...
}
});
}
private void doStuff(){
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("FCM Message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410 /* ID of notification */, notificationBuilder.build());
}
}
FireBaseMsgService.java
public class FireBaseMsgService extends FirebaseMessagingService{
private final String TAG = "FireBaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "test")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setColor(0xffff7700)
.setVibrate(new long[]{100, 100, 100, 100})
.setPriority(Notification.PRIORITY_MAX)
.setSound(defaultSoundUri);
Intent resultIntent = new Intent(this, SplashActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notificationBuilder.build());
}
}
这是添加到AndroidManifest.xml文件中的内容
<service android:name=".FireIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".FireBaseMsgService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
这是要执行的Notify类
public class Notify extends AsyncTask<Void,Void,Void>{
private String tkn;
private String title;
private String body;
public Notify(String tkn, String title, String body){
this.tkn = tkn;
this.title = title;
this.body = body;
}
@Override
protected Void doInBackground(Void... voids) {
Log.e("Token: ", tkn);
Log.e("Title: ", title);
Log.e("Body: ", body);
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key=KEY_HERE");
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to", tkn);
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
info.put("priority", "high");
info.put("show_in_foreground", "true");
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
catch (Exception e)
{
Log.d("Error",""+e);
}
return null;
}
}
答案 0 :(得分:1)
如果您使用的是android 8.0+。您需要指定channelId进行通知。 当您的应用程序处于后台运行时(如您在第一种情况中提到的那样),该推送通知将在系统通知托盘中接收,而不是在FireBaseMsgService中,并且系统会自动通过channelId对其进行处理,该ChannelId由系统本身生成。 当您的应用程序处于前台(第二种情况)时,将执行FireBaseMsgService,并且必须创建通知channelId