hello i am using the gcm push notification
for my app. i am sending project id
to server and getting the user id
from server successfully. also server sends the push message
successfully i checked it, but my device application does not receiving any message... what would be the problem?. thanks
GCMIntentService.class
package com.yash.rastiyaujjalaapps;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GCMIntentService extends IntentService{
Context context;
public static final int NOTIFICATION_ID =1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public int when=(int)System.currentTimeMillis();
public int per=when%100;
private Handler handler;
// System.out.println("...."+ when);
public GCMIntentService() {
super("237330017668");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(){
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
System.out.println("...."+ msg);
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
String msg2 = extras.getString("message");
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
System.out.println("...."+ msg);
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
String msg1=msg2.trim();
sendNotification(msg1);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg1) {
//msg=msg.trim();
long[] vibraPattern = {0, 500, 250, 500 };
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// int when=(int)System.currentTimeMillis();
//int per=when%100;
// System.out.println("...."+ when);
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Intent notificationIntent = new Intent(context, MainActivity.class);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
// | Intent.FLAG_ACTIVITY_SINGLE_TOP);*/
Intent myintent = new Intent(this, MainActivity.class);
myintent.putExtra("message", msg1);
// myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Rashtriya Ujala News")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg1))
.setContentText(msg1)
.setVibrate(vibraPattern)
.setLights(0xff00ff00, 300, 100)
.setSound(defaultSound)
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
//mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
mNotificationManager.notify(per, mBuilder.build());
// Update the TextView
// TextView text = (TextView) findViewById(R.id.textView1);
// text.setText("This app has been started " + counter + " times.");
// Increment the counter
}
}
GCMBroadcastReceiver.class
package com.yash.rastiyaujjalaapps;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
public void onReceive(Context mcontext, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(mcontext.getPackageName(),
GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(mcontext, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Manifestfile is
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yash.rastiyaujjalaapps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.yash.rastiyaujjalaapps.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.yash.rastiyaujjalaapps.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name1"
android:theme="@style/Theme.Sherlock.Light" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.yash.rastiyaujjalaapps.SplashScreen"
android:theme="@style/Theme.Sherlock.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".GCMBroadcastReceiver"
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.yash.rastiyaujjalaapps" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<activity
android:name="com.yash.rastiyaujjalaapps.HomeMainScreen"
android:theme="@style/Theme.Sherlock.Light.NoActionBar" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.MainActivity"
android:icon="@drawable/mono"
android:label="@string/app_name1" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.LiveVideo"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/rastiyaujjala"
android:uiOptions="splitActionBarWhenNarrow" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.PoliticalMenu"
android:label="@string/political"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.AgricultureMenu"
android:label="@string/agriculture"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.SportMenu"
android:label="@string/sport"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.HomeDetails"
android:icon="@drawable/mono" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Home"
android:label="Home" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.National"
android:label="@string/national" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.National2"
android:icon="@drawable/mono"
android:label="@string/national" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.International2"
android:icon="@drawable/mono"
android:label="@string/internatation" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Business2"
android:icon="@drawable/mono"
android:label="@string/business" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Rajya2"
android:icon="@drawable/mono"
android:label="@string/business" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Political2"
android:icon="@drawable/mono"
android:label="@string/political" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Technology2"
android:icon="@drawable/mono"
android:label="@string/technology" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Lifestyle2"
android:icon="@drawable/mono"
android:label="@string/lifestyle" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Rashifal2"
android:icon="@drawable/mono"
android:label="@string/rashifal" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.Sports2"
android:icon="@drawable/mono"
android:label="@string/sport" >
</activity>
<activity
android:name="com.yash.rastiyaujjalaapps.VideoPageFragment"
android:label="@string/sport"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<!--
<receiver android:name="com.personagraph.sensor.service.StartupReceiver"> <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/> </intent-filter>
</receiver>
<service android:name="com.personagraph.sensor.service.SensorService">
<intent-filter>
<action android:name="com.personagraph.SensorService" />
</intent-filter>
</service>
-->
</application>
在您的AndroidManifest.xml中,将android:name =“。GCMBroadcastReceiver”替换为android:name =“ com.yash.rastiyaujjalaapps.GCMBroadcastReceiver”,并将* service android:name =“。GCMIntentService” * 替换为服务android:name =“ com。 yash.rastiyaujjalaapps.GCMIntentService“
In new version of google android play service have so much changed with the previous version. You can refer this tutorial (step by step) of google. It's simple and easy to understand. Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.