小编Los*_*nos的帖子

当应用程序未处于活动状态时,Android 通知声音不播放

所以,我通过网络搜索,并没有看到任何人遇到我遇到的这个问题。

我正在构建一个需要推送通知的应用程序。推送通知显示在状态栏和锁定屏幕上,但仅在应用程序处于活动状态时发出声音。我不知道我错过了什么。

这是我的代码:

public class PushListenerService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.d("FROM", from);
        Bundle pushNotification = (Bundle) data.get("notification");
        String title = pushNotification.get("title").toString();
        String body = pushNotification.get("body").toString();
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder notification = new Notification.Builder(this);

        notification.setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(R.drawable.portrait_front)
                    .setSound(alarmSound);

        notificationManager.notify(0, notification.build());
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

编辑:添加清单以供进一步检查:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sth.idapp.android.privateapp">

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme">

    <activity
        android:screenOrientation="portrait"
        android:name=".start.StartActivity"
        android:theme="@style/AppTheme.HideActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> …
Run Code Online (Sandbox Code Playgroud)

android push-notification lockscreen

5
推荐指数
1
解决办法
2037
查看次数

标签 统计

android ×1

lockscreen ×1

push-notification ×1