当应用未激活时,Android通知声音无法播放

时间:2015-12-15 09:16:16

标签: android push-notification lockscreen

所以,我在网上搜索过,并没有看到有人遇到过这个问题。

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

这是我的代码:

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());
    }
}

非常感谢任何帮助!

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

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

    <activity android:name=".main.MainActivity"
        android:screenOrientation="portrait"
        android:label="ID-APPEN"
        android:theme="@style/AppTheme.HideActionBar"/>

    <activity android:name=".styleguide.StyleguideActivity"
        android:screenOrientation="portrait"
        android:label="Styleguide"
        android:parentActivityName=".main.MainActivity" />

    <activity android:name=".profile.ProfileActivity"
        android:screenOrientation="portrait"
        android:label="@string/main_title"
        android:parentActivityName=".main.MainActivity" />

    <activity android:name=".lookups.list.FraudattempsListActivity"
        android:screenOrientation="portrait"
        android:label="@string/main_fraud_attempts"
        android:parentActivityName=".main.MainActivity" />

    <activity android:name=".lookups.details.FraudattemptsDetailsActivity"
        android:screenOrientation="portrait"
        android:label="@string/main_fraud_attempt"
        android:parentActivityName=".lookups.list.FraudattempsListActivity" />

    <activity android:name=".lookups.list.EntriesListActivity"
        android:screenOrientation="portrait"
        android:label="@string/main_entry"
        android:parentActivityName=".main.MainActivity" />


    <activity android:name=".profileguide.ProfileguideActivity"
        android:screenOrientation="portrait"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="Profil"
        android:theme="@style/AppTheme.HideActionBar" />

    <activity android:name=".qrcode.QRCodeActivity"
        android:screenOrientation="portrait"
        android:label="QR Code"
        android:parentActivityName=".main.MainActivity" />

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="sth.idapp.android.privateapp" />
        </intent-filter>
    </receiver>
    <service
        android:name=".gcm.PushListenerService"
        android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
        android:name=".gcm.RegistrationIntentService"
        android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="sth.idapp.android.privateapp.permission.RECEIVE" />
    <uses-permission android:name="com.google.android.c2dm.permission.SEND" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>

1 个答案:

答案 0 :(得分:4)

我解决了!

显然问题是我没有从上游发送数据有效负载的声音。

以下是具有相同问题的任何人的代码。

_gcmApi.SendMessage(request.Uri, 
                    request.ApiKey, 
                    new GCMMessage { 
                        to = registrationToken, 
                        notification = new notification { 
                            body = request.Message, 
                            title = "ID-appen", 
                            icon = "@drawable/portrait_front", 
                            sound = "default" 
                        } 
                     });

感谢您的帮助!