用户在应用程序中选择的Android特定通知声音

时间:2017-03-20 20:04:26

标签: android firebase push-notification firebase-cloud-messaging

在我的应用中,我使用FirebaseMessagingService为推送通知捆绑了一个自定义的独特声音。

我还内置了用户选择自己的自定义声音的能力。

如果使用 在应用 ,则使用以下代码:用户选择的声音播放,如果他们选择了一个。但如果超出应用程序,我的自定义唯一声音将会播放。

我希望用户选择自定义通知声音(如果他们选择了一个),如果他们没有使用应用而不是我独特的声音,则可以播放。

我已将铃声声音URI存储在首选项文件中: content:// media / internal / audio / media / 57

public class MyAppFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCMService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            sendNotification(remoteMessage.getNotification().getBody());
        }

    }

    private void sendNotification(final String messageBody) {
        Intent intent = new Intent(this, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = null;

        if (AppPref.getUserWantsCustomSound()){ 
            //ANDROID SOUND CHOSEN BY USER
            defaultSoundUri = Uri.parse(AppPref.getUserCustomSound()); // Returns content://media/internal/audio/media/57

        } else {
            //PRE PACKAGED SOUND included in app
            defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pushnotify);
        }

       Log.d(TAG, "defaultSoundUri: " + defaultSoundUri);

       NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("My App")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        //IF IN APP SHOW A TOAST MESSAGE
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(),messageBody,Toast.LENGTH_SHORT).show();
            }
        });

    }

}

1 个答案:

答案 0 :(得分:0)

如果您需要在客户端上区分应用程序是处于前台还是后台,请尝试通过Application类实现接口ActivityLifecycleCallbacks,并在应用程序转到后台后设置标记。

public class App extends Application implements Application.ActivityLifecycleCallbacks {

    private static boolean isBackground = false;

    @Override
    public void onActivityStarted(Activity activity) {
        isBackground = false;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        isBackground = true;
    }

    public static boolean isInBackground(){
        return this.isBackground;
    }
}

另外,不要忘记在AndroidManifest.xml文件中定义此自定义应用程序类。

<application
    android:name=".App">
</application>

最后,在您的推送通知代码中:

if (AppPref.getUserWantsCustomSound() && !App.isInBackground()){ 
    //ANDROID SOUND CHOSEN BY USER
    defaultSoundUri = Uri.parse(AppPref.getUserCustomSound()); // Returns content://media/internal/audio/media/57

} else {
    //PRE PACKAGED SOUND included in app
    defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pushnotify);
}