Benefit : we no need to worry about android's version. It will automatically handle version relevant changes.
Following code to generate push-notification
Note : if you want to update notification just use notificationBuilder.
id must be same as time of creation.
Manage backstack from push-notification :
For example you are developing chat app.
Suppose You got message from friend as push-notification. And assume that app is not running. Now if you click on notification it will redirect to chat-detail screen. And if you click back button it should go back to Friend list instead of close app . For this we need to manage task backstack using following code.
1) Suppose ChatMessageActivity is child activity and ChatActivity is parent activity. So by click on push we will open ChatMessageActivity and press back we will redirect to ChatActivity. Add parent and child activity in manifest file.
1) Create object of TaskStackBuilder.
Following code to generate push-notification
public Notification notification; public NotificationCompat.Builder notificationBuilder; PendingIntent pIntent; String message; Intent intent = new Intent(context, MyActivity.class); intent.putExtra("from_notification", true); pIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); Bitmap image = BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon); notificationBuilder = new NotificationCompat.Builder(context) .setContentTitle(context.getString(R.string.app_name)) .setTicker(message) .setContentText(message) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setSmallIcon(R.mipmap.icon) .setLargeIcon(image) .setColor(context.getResources().getColor(R.color.white)) .setContentIntent(pIntent) .setAutoCancel(true); notification = notificationBuilder.build(); notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(id, notification);
Note : if you want to update notification just use notificationBuilder.
notificationBuilder .setTicker(message) .setContentText(message) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setContentIntent(pIntent) .setNumber(totalMessages); notification = notificationBuilder.build(); notificationManager.notify(id, notification);
id must be same as time of creation.
Manage backstack from push-notification :
For example you are developing chat app.
Suppose You got message from friend as push-notification. And assume that app is not running. Now if you click on notification it will redirect to chat-detail screen. And if you click back button it should go back to Friend list instead of close app . For this we need to manage task backstack using following code.
1) Suppose ChatMessageActivity is child activity and ChatActivity is parent activity. So by click on push we will open ChatMessageActivity and press back we will redirect to ChatActivity. Add parent and child activity in manifest file.
<activity android:name=".ChatActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateAlwaysHidden|adjustPan"/> <!-- Add child activity --> <activity android:name=".ChatMessageActivity" android:launchMode="singleTask" android:parentActivityName=".ChatActivity" android:screenOrientation="portrait"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.app.ChatActivity"/> </activity>
1) Create object of TaskStackBuilder.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
2) Adds the back stack
stackBuilder.addParentStack(ChatMessageActivity.class);
3) Adds the Intent to the top of the stack
stackBuilder.addNextIntent(intent); // Here intent is point to activity which we want to open
4) get pending intent from stack builder and use that in above code.
pIntent = stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
No comments :
Post a Comment