我正在使用fcm在我的android應用程序中獲取推送通知。我收到的通知很好,當應用程序打開和關閉或最小化狀態also.But問題是我沒有得到通知聲音時,應用程序在最小化或關閉狀態。當應用程序關閉或最小化時推送通知不會響起
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refrehedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("refreshedtoken",refrehedToken+"");
PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.DEVICETOKEN,refrehedToken);
sendRegistrationToServer(refrehedToken);
}
private void sendRegistrationToServer(String refrehedToken) {
}
}
/////////////////////// ////////////////////////
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size()>0){
Log.d(TAG,"From:"+remoteMessage.getData()+"");
Gson gson = new Gson();
String json = gson.toJson(remoteMessage.getData());
Log.d("jsonresponce",json+",,,");
try {
JSONObject object = new JSONObject(json);
String title = object.getString("title");
String message = object.getString("message");
Log.d("firebasenotification",title+",,"+message);
sendNotificatin(title,message);
} catch (JSONException e) {
e.printStackTrace();
}
}else if(remoteMessage.getNotification()!=null){
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle());
sendNotificatin(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
}
private void sendNotificatin(String title, String message) {
Intent intent = new Intent(this,SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultsoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_ic)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int num = random.nextInt(99999-1000)+1000;
notificationManager.notify(num,notificationBuilder.build());
}
}
/////////////////// /////////////////
<service android:name=".Fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Fcm.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/app_ic" />
如果您想獲得幫助,請添加您的代碼! –
請檢查密碼 –
我測試過「.setSound(defaultsoundUri)」也.. –