我在gcmlistener服务中收到消息,当app在后台时,我会收到通知,但是当我点击它时,我的意图并没有在我的主要活动中调用OnNewIntent。这是我做过的详细信息
public class MyGcmPushReceiver extends GcmListenerService {
private static final String TAG = MyGcmPushReceiver.class.getSimpleName();
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(String from, Bundle bundle) {
String title = bundle.getString("title");
String message = bundle.getString("message");
String image = bundle.getString("image");
String timestamp = bundle.getString("timestamp");
Log.e(TAG, "From: " + from);
Log.e(TAG, "Title: " + title);
Log.e(TAG, "message: " + message);
Log.e(TAG, "image: " + image);
Log.e(TAG, "timestamp: " + timestamp);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
.
.
} else {
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.setAction(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
pushNotification.putExtra("PushReceive","PushReceive");
pushNotification.putExtra("message", message);
pushNotification.putExtra("title", title);
pushNotification.putExtra("timestamp",timestamp);
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
}
}
/**
* Showing notification with text only
*/
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
}
NotificationUtils.class
public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
// notification icon
final int icon = R.mipmap.ic_launcher;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
int requestID = (int) System.currentTimeMillis();
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
requestID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
if (!TextUtils.isEmpty(imageUrl)) {
.
.
.
} else {
showSmallNotification(mBuilder, icon, title, message, timeStamp, resultPendingIntent, alarmSound);
playNotificationSound();
}
}
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
if (Config.appendNotificationMessages) {
// store the notification in shared pref first
MyApplication.getInstance().getPrefManager().addNotification(message);
// get the notifications from shared preferences
String oldNotification = MyApplication.getInstance().getPrefManager().getNotifications();
List<String> messages = Arrays.asList(oldNotification.split("\\|"));
for (int i = messages.size() - 1; i >= 0; i--) {
inboxStyle.addLine(messages.get(i));
}
} else {
inboxStyle.addLine(message);
}
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Config.NOTIFICATION_ID, notification);
}
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hdiz.designnotification" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_TASKS" />
<permission
android:name="hdiz.designnotification.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="hdiz.designnotification.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.DesignDemo">
<activity
android:noHistory="true"
android:launchMode="singleTask"
android:name=".MainActivity"
android:label="@string/app_name"
>
</activity>
<activity android:name=".NotifDetailActivity"
android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</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" />
<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" />
<category android:name="hdiz.designnotification" />
</intent-filter>
</receiver>
<service
android:name="gcm.MyGcmPushReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="gcm.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="gcm.GcmIntentService"
android:exported="false"></service>
</application>