其他分享
首页 > 其他分享> > 应用未激活时未播放Android通知声音

应用未激活时未播放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());
    }
}

任何帮助将不胜感激!

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

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

解决方法:

我解决了!

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

这是任何有相同问题的人的代码.

_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" 
                        } 
                     });

感谢您的所有帮助!

标签:lockscreen,push-notification,android
来源: https://codeday.me/bug/20191027/1945569.html