I am using online tool to send Notification to my Device
They both returns,
Failure: HTTP Status Code: 401 -Unauthorized
I have followed following steps to implement Google Clound Messaging API
- Create Configuration File added it in my Project->app Folder
Created
Async Task
In Main Activity and write Following Code.SharedPreferences getPreference= getSharedPreferences("GCMToken", Context.MODE_PRIVATE); String tokenID= getPreference.getString("tokenID","");
if(tokenID.isEmpty()) { InstanceID gcmTokenistanceID = InstanceID.getInstance(getApplicationContext()); token = gcmTokenistanceID.getToken(getString(R.string.project_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); GCMServiceURL= "MyWebServerURL.com/="+token; Log.i("TOKEN From IF Condition", token); saveTokenID(token);
Then I created NotificationService and extend this class with GcmListenerService
public class GCMNotificationService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("Test Message from Server");
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText(message);
notificationManager.notify(1, mBuilder.build());
}
Manifest File
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.app.myapp.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.app.myapp.permission.C2D_MESSAGE" />
<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="com.app.myapp" />
</intent-filter>
</receiver>
<service
android:name="com.app.myapp.GCMNotificationService" android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
Kindly guide me what i am doing wrong here