我坚持将GCM推送通知集成到Android应用程序中。我获得了设备ID,服务器端似乎没问题,因为它给出了成功消息。我认为问题在于android部分。请检查我的代码。 我的清单文件在下面提供。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.purpletab.sanghi">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<permission android:name="com.purpletab.sanghi.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.purpletab.sanghi.permission.C2D_MESSAGE" />
<application
android:name=".BaseApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="@style/AppTheme"
>
<activity
android:name=".activities.SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.DashActivity"
android:label=""
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"/>
<activity
android:name=".activities.CoreActivity"
android:label=""
android:parentActivityName=".activities.DashActivity"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.purpletab.sanghi.activities.DashActivity" />
</activity>
<activity android:name=".activities.SignInActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
/>
<activity android:name=".activities.OtpActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"></activity>
</application>
<receiver
android:name="com.google.android.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.purpletab.sanghi" />
</intent-filter>
</receiver>
<service android:name="com.purpletab.sanghi.GcmMessageHandler" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>
下面提供了GCMBroadcastReceiver文件
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
GcmBroadcastReceiver()
{
System.out.println("RECEIVER IS WORKING...");
}
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
System.out.println("RECEIVED NOTIFICATION............");
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMMessageHandler类在下面给出
public class GcmMessageHandler extends IntentService implements Observer, AppConstants {
String mes;
private Handler handler;
//private PushReceivingTemplate details;
private BaseApp baseApp;
public GcmMessageHandler() {
super("GcmMessageHandler");
System.out.println("HANDLER IS WORKING...");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
baseApp = (BaseApp) getApplication();
baseApp.getObserver().addObserver(this);
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("message");
/*
Gson gson = new Gson();
Type collectionType = new TypeToken<PushReceivingTemplate>() {
}.getType();
details = gson.fromJson(mes, collectionType);
final DatabaseHandler dbh = new DatabaseHandler(this);
dbh.addNotidfications(details);
//mes = getResources().getString(R.string.gcmPush);
if(baseApp.getObserver().isAppIsInBackground(this))
{
NotificationUtils.displayNotification(this, details, baseApp);
}
//}*/
System.out.println("____________________" + mes);
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void update(Observable observable, Object data) {
}
}
我从服务器收到此消息
{"multicast_id":6519739741263216481,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1457419626958900%8c03edcaf9fd7ecd"}]}
请解决问题。
答案 0 :(得分:1)
您正在收到服务器消息,因此无法保留任何配置问题。
保留生成通知代码:
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("message");
/*
Gson gson = new Gson();
Type collectionType = new TypeToken<PushReceivingTemplate>() {
}.getType();
details = gson.fromJson(mes, collectionType);
final DatabaseHandler dbh = new DatabaseHandler(this);
dbh.addNotidfications(details);
//mes = getResources().getString(R.string.gcmPush);
if(baseApp.getObserver().isAppIsInBackground(this))
{
NotificationUtils.displayNotification(this, details, baseApp);
}
//}*/
System.out.println("____________________" + mes);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText(mes);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
希望这会对你有所帮助。
答案 1 :(得分:0)
确保您的GcmMessageHandler放置在您在权限中定义的同一个包中,即com.purpletab.sanghi