Reputation: 155
Sorry for my english. I use GCM for notification, server create json use standart GCM form this. Problem: when app is open and i have notification, evrithing fine all intent and put extra work good. But when app is closed, and i get notification, when i click from this notification open standart activity in app.(not activity what i set) This is due to the fact that, android if app is closed created own notification and he dont know about enother intent and activity, he open standart activity in app. The solution to this problem need send from server click_action
, and in manifest need do something like this:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Qestion: how i cant use putExtra
when android create own push?(not use my class for notification)
UPD: class create notification(problem not here):
public class GcmMessageHandler extends GcmListenerService {
static int count = 0;
@Override
public void onMessageReceived(String from, Bundle data) {
String name = notification.getString("title");
String message = notification.getString("body");
Resources res = ctx.getResources();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire(15000);
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icon));
mBuilder.setVibrate(new long[]{1000, 1000});
mBuilder.setTicker(name + ":" + message);
mBuilder.setContentText(message);
mBuilder.setContentTitle(name);
mBuilder.setAutoCancel(true);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
Intent i = new Intent(getApplicationContext(), Activity2.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
ctx,
count,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager myNotificationManager = (NotificationManager)
ctx.getSystemService(ctx.NOTIFICATION_SERVICE);
myNotificationManager.notify(count, mBuilder.build());
}
}
UPD Manifest(when i use permission and GCM)
<!-- GCM -->
<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.GET_ACCOUNTS" />
<permission android:name="myapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="myapp.permission.C2D_MESSAGE" />
<!-- GCM END -->
<!-- GCM -->
<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" />
<category android:name="myapp" />
</intent-filter>
</receiver>
<service android:name="push.GcmMessageHandler"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM -->
Upvotes: 1
Views: 1002
Reputation: 1794
You can use this one to send notification
private void sendNotification(String from, String message) {
Bundle bundle = new Bundle();
bundle.putString("name", from);
bundle.putString("message", message);
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("INFO", bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(from)
.setSound(defaultSoundUri)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
And don't forget to add WAKE_LOCK permission in Manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Upvotes: 1