getting null device id while registering to gcm

前端 未结 2 1299
你的背包
你的背包 2020-11-29 12:59
GCMRegistrar.checkDevice(this);
GCMRegistrar.unregister(this);  
GCMRegistrar.checkManifest(this);

if (GCMRegistrar.isRegistered(this)) {
    GCMRegistrar.getRegist         


        
2条回答
  •  没有蜡笔的小新
    2020-11-29 13:24

    you have to import google play services project from the sdk into your workspace and add it as a lib to your project.then follow the below steps:

    1).//declare global
        private GoogleCloudMessaging _gcm;
        String _devideId;
    2). then execute a new thread like this:
    private class GetDeviceID extends AsyncTask{
        @Override
        protected void onPreExecute() {
            // progress bar
            super.onPreExecute();
        }
    
    
    
        @Override
        protected String doInBackground(String... params) {
            if (_gcm == null) {
                _gcm = GoogleCloudMessaging.getInstance(CompanyRegistration.this);
            }
    
            try {
                _devideId= _gcm.register("your project number of 12 digit");
                System.out.println("my id is\n" + _devideId+ _devideId.length());
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
    
            //dismiss dialouge here
    
        }
    }
    3). in _devideId String you get the device id.
    4).modify your manifest file as
    
    
    
    
    
    
    
    
    
    
    

    //in application tag add your reciever

        
            
                
    
                
            
        
    

    // broadcast reciever class

      import android.app.Activity;
       import android.app.NotificationManager;
        import android.app.PendingIntent;
        import android.content.BroadcastReceiver;
        import android.content.Context;
       import android.content.Intent;
         import android.content.SharedPreferences;
       import android.media.RingtoneManager;
       import android.support.v4.app.NotificationCompat;
        import android.util.Log;  
    
    import com.google.android.gms.gcm.GoogleCloudMessaging;
    
    public class GcmBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "GCMDemo";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context ctx;
    private SharedPreferences prefs;
    Boolean alert = true;
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        prefs = context.getSharedPreferences(
                GcmBroadcastReceiver.class.getSimpleName(),
                context.MODE_PRIVATE); // changes here
    
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        ctx = context;
        String messageType = gcm.getMessageType(intent);
        System.out.println("reciever is" + messageType);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification("Send error: " + intent.getExtras().toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + intent.getExtras().toString());
        } else {
    
            Log.i("aaaaaa", intent.getExtras().toString());
            sendNotification(intent.getExtras().getString("message").toString());
        }
        setResultCode(Activity.RESULT_OK);
    }
    
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);
    
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                new Intent(ctx, GcmBroadcastReceiver.class), 0); // changes here
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                ctx).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Come Fetch Me Notification ")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);
    
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
    }
    

提交回复
热议问题