应用关闭或最小化时推送通知不会响起

时间:2017-03-11 15:40:39

标签: android audio push-notification

我使用fcm在我的Android应用中获取推送通知。当应用程序打开和关闭或最小化状态时,我收到通知。但问题是当应用程序处于最小化或关闭状态时,我没有收到通知声音。

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" />

2 个答案:

答案 0 :(得分:3)

我没有足够的声誉来添加评论,所以,我正在为你做这个,但我知道这是不合适的。

原因是当应用最小化时,或者在后台,将通知发送到Android通知托盘的通知。有两个有效载荷,一个是&#34;数据有效载荷&#34;另一个是&#34;通知有效载荷。&#34;只需删除&#34;通知有效负载&#34;然后它会正常工作。

答案 1 :(得分:0)

我编辑了我的代码,你忘了添加.setSound。我还添加了一些你将来可能想要的东西。

Code