0
votes

I have researching for hour and hour and found many similar question but have no a working solution for my question.

I'm understand if I close the app by force close that is normal that I not receive data from gcm. But I was unable to receive the notification with gcm until I start the app. So the application is running either foreground or background then I only be able to receive the data from gcm but when I close the app by sliding away it (which is not done by force close) the notification didn't show up which mean gcm wasn't receive any data at all. . So it mean I need to stay active but if I do so then there is no point for using gcm anymore which design for pushing notification.

Here is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.khooteckwei.google" >

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:name="com.example.gcmtesting.google.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.gcmtesting.google.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.gcmtesting.google" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmIntentService" />
    </application>

</manifest>

here is the wakefulBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

The notification working fine when the app is active so I didn't include the function that use to display notification.Is there any problem with this 2 pages?

1
Perhaps your device performs a force-stop when you are "sliding away it". That is not normal behavior, but there are at least a few devices where the manufacturer screwed up and did this. Try running your app, "sliding away it", then looking at the app's page in Settings > Apps. If the "Force Stop" button is disabled, then you know that "sliding away it" does a force-stop.CommonsWare
Thx for reply. But the button is enable so I don't think is that issueuser3651999
Just to clarify, do you mean that you are only able to receive gcm notifications if your app is in foreground?AL.
@sept actually not. Either foreground or background as long as the app didn't completely by swiping away or clear task at task manager. The notifications arrive. If i just simply reboot the phone and didn't lauch the app I will not receive any gcm notification.user3651999
@sept er.. The gcm working fine in emulator but in real device i have been test Oppo N7, samsung s7 all didn't work for meuser3651999

1 Answers

0
votes

I think your problem is in your GcmBroadcastReceiver receiver, you can look at this link , I think it's will help you with GCM http://www.intertech.com/Blog/push-notifications-tutorial-for-android-using-google-cloud-messaging-gcm/

I think notifications doesn't work because your receiver doesn't check do your app running or not , if it is you need to handle notification or if not show android notification