Skip to content Skip to sidebar Skip to footer

Gcm Couldnt Receive Message

Server: var GCM = require('gcm').GCM; var apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; var gcm = new GCM(apiKey); var message = { registration_id: 'APA91bEAjoFkJu6KF-UgbLWhB-qbHs

Solution 1:

Check This code It may be help you :

protectedvoidgcm() {
    // TODO Auto-generated method stubGCMHelpergcmhelper=newGCMHelper();
    StringgcmtokenAlready= gcmhelper.alreadyregisterGCM();
    if(gcmtokenAlready.length()>0){

        To do code here

    }else{
        gcmhelper.registerGCM();
    }

//GCMHelper

publicclassGCMHelper{
privatefinalString SENDER_ID = ContextProvider.getContext().getResources().getString(R.string.GCM_ID);
privatefinal Context context = ContextProvider.getContext();
/**
 *  This method is used to register the device with the GCM.
 *
 * @param context the context
 */publicvoid registerGCM() {
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);        
    GCMRegistrar.register(context, SENDER_ID);
}

/**
 * Already register gcm.
 *
 * @param context the context
 * @return the string
 */publicString alreadyregisterGCM() {
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    return GCMRegistrar.getRegistrationId(context);
}

}

//GCMIntentService //Note Put this class in main package of your Application.

publicclassGCMIntentServiceextendsGCMBaseIntentService {

/**
 * Instantiates a new GCM intent service.
 */publicGCMIntentService() {
    super(ContextProvider.getContext().getResources().getString(R.string.GCM_ID));
}

/*
 * (non-Javadoc)
 * 
 * @see
 * com.google.android.gcm.GCMBaseIntentService#onError(android.content.Context
 * , java.lang.String)
 */@OverrideprotectedvoidonError(Context arg0, String arg1) {
    // TODO Auto-generated method stubLog.d(TAG, "Error: " + "sError");
}

/*
 * @see Used when message comes form GCM server. Call your notification
 * class here
 *//* (non-Javadoc)
 * @see com.google.android.gcm.GCMBaseIntentService#onMessage(android.content.Context, android.content.Intent)
 */@OverrideprotectedvoidonMessage(Context context, Intent arg1) {
    // TODO Auto-generated method stubString notiData = arg1.getStringExtra("message");
    /**Start BroadCast*/Intent mintent = newIntent("PAYMENT_STATUS_MESSAGE");
    mintent.putExtra("PAYMENT_STATUS_MESSAGE", notiData);
    //Send BroadCastsendBroadcast(mintent);
    //Generate NotificationgenerateNotification(context, notiData);
}

/*
 * (non-Javadoc)
 * 
 * @see
 * com.google.android.gcm.GCMBaseIntentService#onRegistered(android.content
 * .Context, java.lang.String)
 */@OverrideprotectedvoidonRegistered(Context context, String token) {
    /** Saving the device token in the Application Class */Log.v(TAG, "Registered First Time");

}


/* (non-Javadoc)
 * @see com.google.android.gcm.GCMBaseIntentService#onUnregistered(android.content.Context, java.lang.String)
 */@OverrideprotectedvoidonUnregistered(Context arg0, String arg1) { }

/**
 * Issues a notification to inform the user that server has sent a message.
 *
 * @param context the context
 * @param message the message
 */privatevoidgenerateNotification(Context context, String message) {
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)   context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = newNotification(R.drawable.ic_launcher, message, when);         
    String title = "New POC Alert";         
    Intent notificationIntent = newIntent(context, null);
    // 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;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

}

//Mainfest Config

<!-- GCM Permission --><uses-permissionandroid:name="android.permission.GET_ACCOUNTS" /><uses-permissionandroid:name="android.permission.WAKE_LOCK" /><uses-permissionandroid:name="com.google.android.c2dm.permission.RECEIVE" /><uses-permissionandroid:name="android.permission.VIBRATE" /><permissionandroid:name="com.sc.candidate.permission.C2D_MESSAGE"android:protectionLevel="signature" /><uses-permissionandroid:name="com.sc.candidate.permission.C2D_MESSAGE" /><uses-permissionandroid:name="com.google.android.c2dm.permission.RECEIVE" /><uses-permissionandroid:name="vibrate" /><!-- For GCM --><receiverandroid:name="com.google.android.gcm.GCMBroadcastReceiver"android:permission="com.google.android.c2dm.permission.SEND" ><intent-filter><actionandroid:name="com.google.android.c2dm.intent.RECEIVE" /><actionandroid:name="com.google.android.c2dm.intent.REGISTRATION" /><categoryandroid:name="com.sc.candidate" /></intent-filter></receiver><serviceandroid:name=".GCMIntentService" />

*Note please change the package name According to your application main package.

Post a Comment for "Gcm Couldnt Receive Message"