1

Here is my full code, in this. If the app is running, then i'll get push notification that too for public void handleResponse method. But if i quit the app, i wont get push notification itself, please help.

Mainactivity.java

    public class MainActivity extends AppCompatActivity {

    private static final String YOUR_APP_ID = "2C25D658-240D-4350-FF8A-6CF4DBD88F00";
    private static final String YOUR_SECRET_KEY = "24DB6F44-8C97-1DCF-FF22-060777780600";
    String appVersion = "v1";
    Subscription subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Backendless.initApp(this, YOUR_APP_ID, YOUR_SECRET_KEY, "v1");

        Backendless.Messaging.registerDevice("183254839430", "default", new AsyncCallback<Void>() {
            @Override
            public void handleResponse(Void aVoid) {

                Toast.makeText(MainActivity.this, "Registered", Toast.LENGTH_LONG).show();
            }

            @Override
            public void handleFault(BackendlessFault backendlessFault) {

                Toast.makeText(MainActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

        Backendless.Messaging.subscribe( "default",
                new AsyncCallback<List<Message>>() {

                    @Override
                    public void handleResponse(List<Message> response) {

                        Toast.makeText(MainActivity.this, "Notification arrived successfully", Toast.LENGTH_LONG).show();

                        for (Message message : response) {
                            String publisherId = message.getPublisherId();
                            Object data = message.getData();
                        }
                    }

                    @Override
                    public void handleFault(BackendlessFault fault) {
                        Toast.makeText(MainActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                },
                new AsyncCallback<Subscription>()
                {
                    @Override
                    public void handleResponse( Subscription response )
                    {
                        subscription = response;
                    }
                    @Override
                    public void handleFault( BackendlessFault fault )
                    {
                        Toast.makeText( MainActivity.this, fault.getMessage(), Toast.LENGTH_SHORT ).show();
                    }
                }
        );

    }
}

pushreceiver.java.

This code itself is not triggering, i'm confused why :-(

    public class PushReceiver extends GcmListenerService {

        //This method will be called on every new message received
        @Override
        public void onMessageReceived(String from, Bundle data) {

            Toast.makeText(getApplicationContext(), "Notification came PushReceiver", Toast.LENGTH_LONG).show();

            //Getting the message from the bundle
            String message = data.getString("message");
            //Displaying a notiffication with the message
            sendNotification(message);
        }

        //This method is generating a notification and displaying the notification
    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        int requestCode = 0;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
    }
}

My Manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="feelzdroid.backednlessgcm">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="feelzdroid.backednlessgcm.permission.C2D_MESSAGE"/>
<permission android:name="feelzdroid.backednlessgcm.permission.C2D_MESSAGE" android:protectionLevel="signature"/>


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

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

    <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"/>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
            <category android:name="feelzdroid.backednlessgcm"/>
        </intent-filter>
    </receiver>

    <service android:name=".PushReceiver" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
        </intent-filter>
    </service>

</application>

Log.

07-03 16:43:31.893 11026-11143/feelzdroid.backednlessgcm I/GMPM: App measurement is starting up
07-03 16:43:31.915 11026-11143/feelzdroid.backednlessgcm E/GMPM: getGoogleAppId failed with status: 10
07-03 16:43:31.918 11026-11143/feelzdroid.backednlessgcm E/GMPM: Uploading is not possible. App measurement disabled
Naruto
  • 9,476
  • 37
  • 118
  • 201
  • Did you check your debug logs ? While running your app, check the logs if GCM registration is done successfully or not. Most of the time logs tell you if there is any problem. Do the same while sending teh push notification. Send a push to your device, with device connected , so that you can check the log whether it is received or not. Here logs mean the device logs and not necessarily your app's log. – intellignt_idiot Jul 03 '16 at 10:51
  • When my app is on, if i send push notification from the server, handleResponse method gets called. As it comes with in the `Backendless.Messaging.subscribe`. But if i quit the app completely, i'm not getting the notification. Does it really require to subscribe for channel to trigger notification?. Without subscribe method also, it should work right? – Naruto Jul 03 '16 at 10:55
  • Check the logs, and see for red lines, errors or warnings , they will tell you if anything is wrong. Check for the Android logs, not app logs. Also, enable the logs for the library that you are using for GCM. – intellignt_idiot Jul 03 '16 at 10:57
  • i have pasted the Log, see the above log.... – Naruto Jul 03 '16 at 11:06
  • The 3 lines of log tell nothing. You have to check yourself if there is any red lined log or warning log appear during start of app or during the post notification. Simplest thing to confirm this is, by checking the logs when you really recieve push notification, when your app is opened. So check the logs with your app opened by sending push and then check again after closing the app and see if any of the logs say anything helpful. Any by the way, what library are you using for GCM ? – intellignt_idiot Jul 03 '16 at 11:14
  • I'm using Backendless services to communicate between GCM & my APP. Backendless does the storing and maintaining all device ID & etc... this works very well with no issues. But sending notification is the biggest issue – Naruto Jul 03 '16 at 11:20
  • Check the documentation again, and see if you missed anything. Otherwise you should ask the backendless support team. It looks like this is the issue from their end. Good luck – intellignt_idiot Jul 03 '16 at 11:28
  • You may want to check [Launch controls on stopped applications](https://developer.android.com/about/versions/android-3.1.html#launchcontrols), this explains that "Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications." Also here are some SO answers you might want to read: [1](http://stackoverflow.com/a/20838611/5995040) and [2](http://stackoverflow.com/a/37429495/5995040) – Mr.Rebot Jul 03 '16 at 23:54

0 Answers0