你好,我正在使用gcm push notification
作为我的应用。我正在向服务器发送project id
并成功从服务器获取user id
。服务器也成功发送push message
我检查了它,但我的设备应用程序没有收到任何消息......会出现什么问题?感谢
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是
<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>
答案 0 :(得分:0)
在AndroidManifest.xml中,将 android:name =“。GCMBroadcastReceiver”替换为 android:name =“com.yash.rastiyaujjalaapps.GCMBroadcastReceiver” 和* service android:name =“。GCMIntentService”* by service android:name =“com.yash.rastiyaujjalaapps.GCMIntentService”
答案 1 :(得分:0)
在新版本的谷歌Android播放服务与以前的版本有很大的变化。您可以参考谷歌的this tutorial(一步一步)。它简单易懂。祝你好运!