5

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>
LosGlennos
  • 99
  • 10
  • try to set volume to max – koutuk Dec 15 '15 at 09:25
  • 2
    As I stated in the question, sound is fine when the app is active. So the problem is not with the volume of sound on the phone. – LosGlennos Dec 15 '15 at 09:28
  • Is there a way you can test in another phone? You seem to be doing everything right so I think it might be some setting of the phone you are testing on. Otherwise can you change setSound for setDefaults(Notification.DEFAULT_ALL)? Just for testing if the default sound works. – Miguel Garcia Dec 15 '15 at 16:07
  • @MiguelGarcia I tried with another phone but no luck unfortunately. I also tried commenting out the ".setSound" and replaced it with ".setDefaults" but no luck there either. Maybe it has something to do with my Manifest? I have updated the question with my Manifest, do you think you can see something there? – LosGlennos Dec 16 '15 at 06:48
  • on further inspection I see you are using Notification.Builder directly instead of the version provided by NotificationCompat.Builder which applies sensible defaults most of the time. You can switch to that one instead or if for some reason you need to keep it can you try using setSound(sound, Notification.STREAM_DEFAULT). Also make sure there is no notification showing up already when you test the background case (or make sure you don't have setFlag(Notification.FLAG_ONLY_ALERT_ONCE, onlyAlertOnce) setup) – Miguel Garcia Dec 16 '15 at 10:41
  • So I tried changing the Notification.Builder to NotificationCompat.Builder using android.support.v7.app (that should be the correct library right?) but still no success. I tried pushing notification with other notifications present and also when there were no notifications at all active. Did you see anything weird in my manifest that could cause this problem. Thank you for all your help! I really appreciate it! – LosGlennos Dec 16 '15 at 12:23

1 Answers1

4

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!

LosGlennos
  • 99
  • 10