1)我已经实现了推送通知。当设备连接到Internet时工作正常。(i-e)从服务器端接收通知
2)当设备未连接到Internet时。如果服务器发送推送消息。然后当设备连接到Internet ..没有收到推送消息..这是问题
3)它喜欢实现推送消息,如什么应用程序..当设备连接到Internet ..它应该收到推送消息..
public class GcmIntentService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("title");
String post_id=data.getString("id");
boolean pushactive= SharedPreference.getInstance().getBool(this,"pushactive");
if(pushactive)
generateNotification(message,post_id);
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("inside GcmListenerService");
}
public void generateNotification(String message,String post_id)
{
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Intent intent = new Intent(this, NewsDetailActivity.class);
intent.putExtra("isPush", true);
intent.putExtra("post_id",post_id);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(message)
.setContentTitle("EGToday")
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
notificationManager.notify(m, mBuilder.build());
}
}
如何实现这个目标?
这是我的意向服务类。
这是我的表现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.egt">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="com.egt.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.egt.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:name="com.egt.Constants.NetworkCall"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".View.SplashPage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:windowSoftInputMode="stateHidden|adjustPan"
android:name=".View.MainActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".View.NewsDetailActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".View.SearchActivity"
android:screenOrientation="portrait">
</activity>
<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.egt" />
</intent-filter>
</receiver>
<service
android:name="com.egt.PushNotification.GcmIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service android:name="com.egt.PushNotification.GcmRegistration"
android:exported="false" />
</application>
</manifest>