0

I'm using infobip PushNotification im send PushNotification from clientpanel but not recive notifcation and send toast "Error occurred:32" Invalid GCM sender ID . How can i get push notification?

manifest file:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <!-- REQUIRED PERMISSIONS -->
    <!-- Custom permission declared so that only your application can receive your notifications. -->
    <permission
        android:name="com.example.push.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <!-- Previously custom defined permission -->
    <uses-permission android:name="com.example.push.permission.C2D_MESSAGE" />
    <!-- Permission to receive push notifications -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- Connect to the Infobip Push service -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Using push notifications requires a Google account -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- Checks network state -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- REQUIRED PERMISSIONS -->
    <!-- OPTIONAL PERMISSIONS -->
    <!-- Needed for push notifications that contain VIBRATE flag. Optional, but recommended. -->
    <uses-permission android:name="android.permission.VIBRATE" />
    <!-- OPTIONAL PERMISSIONS -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/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>
        <!-- REQUIRED SERVICES -->
        <service android:name="com.infobip.push.lib.InfobipPushService" />
        <!-- REQUIRED SERVICES -->


        <!-- REQUIRED RECEIVERS -->
        <receiver
            android:name="com.infobip.push.lib.InfobipPushReceiver"
            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="com.example.push" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.infobip.push.lib.InternalReceiver" />
        <receiver
            android:name=".MyPushReceiver"
            android:permission="com.example.push.permission.C2D_MESSAGE" >
            <intent-filter>
                <action android:name="com.infobip.push.intent.REGISTERED_FOR_NOTIFICATIONS" />
                <action android:name="com.infobip.push.intent.REGISTRATION_REFRESHED" />
                <action android:name="com.infobip.push.intent.UNREGISTERED_FROM_NOTIFICATIONS" />
                <action android:name="com.infobip.push.intent.NOTIFICATION_RECEIVED" />
                <action android:name="com.infobip.push.intent.NOTIFICATION_OPENED" />
                <action android:name="com.infobip.push.intent.ERROR" />

                <category android:name="com.example.push" />
            </intent-filter>
        </receiver>
        <!-- REQUIRED RECEIVERS -->
        <!-- REQUIRED METADATA TAG -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@+integer/google_play_services_version" />
        <!-- REQUIRED METADATA TAG -->
    </application>
 </manifest>

MyPushReceiver File:

package com.example.push;

import android.content.Context;
import android.widget.Toast;

import com.infobip.push.AbstractPushReceiver;
import com.infobip.push.PushNotification;

public class MyPushReceiver extends AbstractPushReceiver {

    @Override
    public void onRegistered(Context context) {

        Toast.makeText(context, "Successfully registered.", Toast.LENGTH_SHORT)
                .show();
    // TODO Auto-generated method stub
    }

    @Override
    protected void onRegistrationRefreshed(Context context) {
        Toast.makeText(context, "Registration is refreshed.",
            Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNotificationReceived(PushNotification notification,
            Context context) {
        Toast.makeText(context,
                "Received notification: " + notification.toString(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onNotificationOpened(PushNotification notification,
            Context context) {
        Toast.makeText(context, "Notification opened.", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onUnregistered(Context context) {
        Toast.makeText(context, "Successfully unregistered.",
            Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onError(int reason, Context context) {
        Toast.makeText(context, "Error occurred: " + reason, Toast.LENGTH_SHORT)
            .show();
    }
}

MainActivity :

package com.example.push;

import java.util.ArrayList;
import java.util.List;

import com.infobip.push.AbstractPushReceiver;
import com.infobip.push.PushNotification;
import com.infobip.push.PushNotificationManager;
import com.infobip.push.RegistrationData;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
    PushNotificationManager manager ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         manager = new PushNotificationManager(this);
        manager.isDebugModeEnabled();
        manager.initialize("<_>", "<_>", "<_>");
        manager.register();
       // Intent startIntent = new Intent(this, MyPushReceiver.class);
       // this.startService(startIntent);
    }

    private BroadcastReceiver receiver = new AbstractPushReceiver() 
    {

        @Override
        public void onRegistered(Context context) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNotificationReceived(PushNotification notification,
                Context context) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onUnregistered(Context context) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onError(int reason, Context context) {
            // TODO Auto-generated method stub

        }

    };

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.infobip.push.intent.REGISTERED_FOR_NOTIFICATIONS");
        filter.addAction("com.infobip.push.intent.REGISTRATION_REFRESHED");
        filter.addAction("com.infobip.push.intent.UNREGISTERED_FROM_NOTIFICATIONS");
        filter.addAction("com.infobip.push.intent.NOTIFICATION_RECEIVED");
        filter.addAction("com.infobip.push.intent.NOTIFICATION_OPENED");
        filter.addAction("com.infobip.push.intent.ERROR");
        filter.addCategory(getPackageName());

        registerReceiver(receiver, filter); //Register receiver
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver); //Unregister receiver
    }


    public void onSubscriptionClick(View v) {
        String userID = "<senderid>";
        String phoneNumber = "385951112222";
        List channels = new ArrayList();
        channels.add("sport");
        channels.add("news");
        channels.add("weather");

        //Perform registration
        RegistrationData registrationData = new RegistrationData();
        registrationData.setUserId(userID);
        registrationData.setPhoneNumber(phoneNumber);
        registrationData.setChannels(channels);
        manager.register(registrationData); //registrationData is optional
    }        
}
msmolcic
  • 6,407
  • 8
  • 32
  • 56

2 Answers2

1

Do you have a GCMIntentService class? Like below code

public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        // displayMessage(context, getString(R.string.gcm_registered));
        ServerUtilities.register(context, registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        // displayMessage(context, getString(R.string.gcm_unregistered));
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            ServerUtilities.unregister(context, registrationId);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        // String message = getString(R.string.gcm_message);
        String message = intent.getStringExtra("message");

        Log.i("message", "" + message);
        // displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

    @Override
    protected void onDeletedMessages(Context context, int total) {
        Log.i(TAG, "Received deleted messages notification");
        String message = getString(R.string.gcm_deleted, total);
        // displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
        // displayMessage(context, getString(R.string.gcm_error, errorId));
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);
        // displayMessage(context, getString(R.string.gcm_recoverable_error,
        // errorId));
        return super.onRecoverableError(context, errorId);
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_stat_gcm;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

}
delavega66
  • 855
  • 2
  • 23
  • 39
Rıdvan
  • 248
  • 1
  • 3
  • 9
0

Regarding invalid sender ID, there are already some answers here on StackOverflow that may help you... e.g.: Android GCM SENDER_ID, how to get it? or GCM Invalid sender error

Regarding Infobip's Push service, I see that you're calling register() method two times. First time when your Activity is created (without Registration data), second time on button's click (with RegistrationData). You need to do this only once, because once you're registered for push, there is no need to re-register.

Your problem is in calling wrong senderID parameter in initialize("<senderId>", "<applicationId>", "<applicationSecret>") method.

Sender ID = Google project number

See how to obtain your project number here: https://push.infobip.com/docs/platforms/android#androidgcm

Example of your Activity:

package com.example.push;

import java.util.ArrayList;
import java.util.List;

import com.infobip.push.AbstractPushReceiver;
import com.infobip.push.PushNotification;
import com.infobip.push.PushNotificationManager;
import com.infobip.push.RegistrationData;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
    PushNotificationManager manager ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = new PushNotificationManager(this);
        manager.setDebugModeEnabled(true);
        //REPLACE "<senderId>" WITH GOOGLE PROJECT NUMBER! 
        manager.initialize("<senderId>", "<applicationId>", "<applicationSecret>") 
    }

    private BroadcastReceiver receiver = new AbstractPushReceiver() 
    {

        @Override
        public void onRegistered(Context context) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onNotificationReceived(PushNotification notification,
                Context context) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onUnregistered(Context context) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onError(int reason, Context context) {
            // TODO Auto-generated method stub

        }

    };

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.infobip.push.intent.REGISTERED_FOR_NOTIFICATIONS");
        filter.addAction("com.infobip.push.intent.REGISTRATION_REFRESHED");
        filter.addAction("com.infobip.push.intent.UNREGISTERED_FROM_NOTIFICATIONS");
        filter.addAction("com.infobip.push.intent.NOTIFICATION_RECEIVED");
        filter.addAction("com.infobip.push.intent.NOTIFICATION_OPENED");
        filter.addAction("com.infobip.push.intent.ERROR");
        filter.addCategory(getPackageName());

        registerReceiver(receiver, filter); //Register receiver
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver); //Unregister receiver
    }


    public void onSubscriptionClick(View v) {
        String userID = "<senderid>";
        String phoneNumber = "385951112222";
        List channels = new ArrayList();
        channels.add("sport");
        channels.add("news");
        channels.add("weather");

        //Perform registration
        RegistrationData registrationData = new RegistrationData();
        registrationData.setUserId(userID);
        registrationData.setPhoneNumber(phoneNumber);
        registrationData.setChannels(channels);
        if (!manager.isRegistered()) {
            manager.register(registrationData); //registrationData is optional
        }
    }        
}
Community
  • 1
  • 1
terka31
  • 46
  • 3