Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android notification sound not playing when app is not active

So, I have searched through the web and haven't seen anyone come across this problem I have.

I am building an app where there needs to be a push notification. The push notification gets shown in the status bar and on the lock screen but only sounds when the application is active. I do not know what I'm missing.

Here's my code:

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

Any help would be greatly appreciated!

EDIT: adding manifest for further inspection:

<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>
like image 984
LosGlennos Avatar asked Sep 26 '22 22:09

LosGlennos


1 Answers

I solved it!

Apparently the problem was that I did not send a sound with the data-payload from my upstream.

Here is the code for anyone that has the same problem.

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

Thank you for all the help!

like image 160
LosGlennos Avatar answered Oct 11 '22 15:10

LosGlennos