【问题标题】:Push Notification not getting sound when app closed or minimized应用程序关闭或最小化时推送通知没有声音
【发布时间】:2017-08-01 21:40:33
【问题描述】:

我正在使用 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) {

}

}

/////////////////////////////////////// /////////////////////p>

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)”测试过..

标签: android audio push-notification


【解决方案1】:

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

原因是当应用最小化时,或者在后台,通知传递到 Android 通知传递到 Android 通知托盘。有两种有效载荷,一种是“数据有效载荷”,另一种是“通知有效载荷”。只需删除“通知负载”即可正常工作。

【讨论】:

    【解决方案2】:

    我已经编辑了我的代码,你忘了添加 .setSound。我还添加了一些您将来可能需要的其他内容。

    private void sendNotificatin(String title, String message) {
    
    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 sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_ic)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(sound) // changed from SET_DEFAULTS
                .setContentIntent(pendingIntent);
                /* Begin other added stuff */
                .setLights(0xffff0000, 500, 250);
                .setWhen(System.currentTimeMillis())
                /* End other added stuff */
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Random random = new Random();
        int num = random.nextInt(99999-1000)+1000;
        notificationManager.notify(num,notificationBuilder.build());
    
        }
    

    【讨论】:

    • 我做了所有的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2017-07-05
    相关资源
    最近更新 更多