I'm using Firebase notifications to send notifications in my Android App. I want to achieve following:
When user clicks notification, activity of my choice should open and activity should perform some actions automatically which I'll define like loading specific url within webview, etc.
What I did?
In Firebase console, under custom data, I set key as click_action. And value as ttdemo and sent notifications.
In my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutmain" />
<activity android:name=".Timetable">
<intent-filter>
<action android:name="ttdemo"/>
</intent-filter>
</activity>
<activity android:name=".Results" />
<activity android:name=".timetable_downloads" />
<activity android:name=".result_downloads" />
<activity android:name=".eforms" />
<activity android:name=".write" />
<activity android:name=".qpapers" />
<activity android:name=".november2014" />
<activity android:name=".november_2014_downloads" />
<activity android:name=".april_2015" />
<activity android:name=".november_2015" />
<activity android:name=".april_2016"></activity>
</application>
Current scenario:
I'm getting notifications but Timetable activity is not opened.
Instead, the main activity opens.
Try like blow code when your onMessageReceived method called.
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
if (data.containsKey("click_action")) {
String className = data.get("click_action");
Class cls;
try {
cls = Class.forName(className);
Intent i = new Intent(context, cls);
//i.putExtras(extras); // put extras if you required
context.startActivity(i);
}catch(ClassNotFoundException e){
//means you made a wrong input in firebase console
}
}
}
To send a notification with firebase console, put a key-value-pair as custom data like this:
Key: click_action
Value: <fully qualified class name of your activity>
You need to pass panding intent to notification in the onMessageReceived(RemoteMessage remoteMessage) method of class FirebaseMessagingService
Intent resultIntent = new Intent(FirebaseMessagingService.this, MobilActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
and set to notification object as
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setContentIntent(resultPendingIntent)
and method like
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.growth_indus)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(AppConstants.getParamInt(AppConstants.GlobalParamEnum.NOTIFICATION_ID), notification);
}
Finally, I achieved the objective!
This solution worked perfectly for me! http://androidbash.com/firebase-push-notification-android/
Related
please find the code of sendNotfication. I tried lots of things but still not working. Do I need to change in some Mainfest file ?
private void sendNotification(String messageBody) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(requestID/* ID of notification */, notificationBuilder.build());
}
Please find the mainfest file. This is the same that i found example in firbase. but still when i click on notfication it always went to SplashScreen.
I tried the recent comments but still not working.
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:fullBackupContent="#xml/backup_descriptor"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait" />
<!-- [START firebase_service] -->
<service android:name=".FCMTest.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_service] -->
<service
android:name=".FCMTest.MyJobService"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/abc" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<activity android:name="com.example.guptasachin.myapplication.NotificationActivity"></activity>
You are using request Id in many places..
Please check the below working code to open the desired Activity :
Here , activity = CurrentActivity context,
mNotificationId = unique constatnt number like 200,201..e.t.c
Try the below code :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mBuilder.setColor(activity.getResources().getColor(R.color.pricify_red));
mBuilder.setSmallIcon(R.drawable.transparent_img);
} else {
mBuilder.setColor(activity.getResources().getColor(R.color.pricify_red));
mBuilder.setSmallIcon(R.drawable.app_logo);
}
mBuilder.setContentTitle(title)
.setContentText(msg);
Intent i = new Intent();
i.setComponent(new ComponentName(currentActivity.this, DesiredActivity.class));
PendingIntent pIntent = PendingIntent.getActivity(activity, 0, i, 0);
mBuilder.setContentIntent(pIntent);
NotificationManager mNotificationManager =
(NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mNotificationId, mBuilder.build());
Not opening specific activity on notification click when the app is in background/not running
The notification-click starts specified activity only when the app is opened up and the notification-click is performed. If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent.
I saw a solution similar to this problem in this forum did not help unfortunately
MyApplication
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONException;
import org.json.JSONObject;
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this).setNotificationOpenedHandler(new ExampleOpenHandler())
.autoPromptLocation(true).init();
}
private void startNotification() { Log.i("NextActivity", "startNotification");
// Sets an ID for the notification int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.bugununfaaliyet)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(1, mBuilder.build());
}
private class ExampleOpenHandler implements OneSignal.NotificationOpenedHandler {
#Override
public void notificationOpened(OSNotificationOpenResult result) {
JSONObject data=result.notification.payload.additionalData;
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
try{
intent.putExtra("datam",data.getString("1"));
}catch (JSONException e) {
e.printStackTrace();
}
startActivity(intent);
}
}
}
SecondActivity
package blabla.com.blabla;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView dataview;
String getdata;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getdata = getIntent().getExtras().getString("datam");
dataview = (TextView) findViewById(R.id.dataview);
dataview.setText(getdata);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blabla.com.blabla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activity.girisKullaniciAdi"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity.TabbedActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.KameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.Anasayfa" />
<activity
android:name=".Activity.FizikselSayimKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.EkipmanGecmisiKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.EkipmanGecmisiActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.YeniIsEmriKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLE" />
</provider>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Please check out this answer..
private void startNotification() {
Log.i("NextActivity", "startNotification");
// Sets an ID for the notification
int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ldb)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
// StopScript is your Activity Name
I want to start a notification in current Activity which is ChatActivity. When I press the notification, I want to enter the ChatOneActivity.
However I don't want the current ChatActivity finished when I'm in the ChatOneActivity. Because I'm receiving the data and when I press the back button, I want to stay in the ChatActivity.
The point is I do not want the ChatActivity to finish, no matter which Activity I am currently in.
So what should I do?
Here is the code
private void showNotification(String id, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.sym_action_email)
.setContentTitle("You have a new message")
.setContentText(message);
Intent intent = new Intent(ChatActivity.this,ChatOneActivity.class);
intent.putExtra("toId", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
The problem is:
Now I enter the ChatOneActivityand when I press the back button, I return to the desktop. Means that the ChatActivity has already finished which I don't want.
Here is the mainfest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.webmobilegroupchat.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatActivity"
android:label="#string/title_activity_chat" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatOneActivity"
android:label="#string/title_activity_chat_one" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.SplashActivity"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Try this on ChatOneActivity activity .
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(),ChatActivity.Class);
startActivity(intent);
}
your code is looking fine for creating notification but you have to define parent activity in Manifest also to navigate back, stackBuilder.addParentStack assumes that you define it in Manifest as well for API 16+:
<activity android:name=".ChatOneActivity"
android:parentActivityName=".ChatActivity"
... />
for < API 16:
<activity android:name=".ChatOneActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ChatActivity" />
Whenever a gcm message comes back to my phone, I receive a notification. However, when I press it, it doesn't open my app. It only disappears. Previously when I used PendingIntent.FLAG_ONE_SHOT, it would work, but if I sent multiple messages, it wouldn't be up to date. Please help.
This is my notification code
public class GCMNotificationIntentService extends IntentService{
//set ID for the notification, so it can be updated
public static final int notifyID = 9001;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
Log.d("GCMN","GCMNTEST");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
sendNotification("Message Received from Google GCM Server:\n\n"
+ extras.get(AppConstants.MSG_KEY));
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg){
Intent resultIntent = new Intent(this, HomeActivity.class);
Log.d("RECEIVEDPT2",msg);
resultIntent.putExtra("msg", msg);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder;
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyBuilder = new NotificationCompat.Builder(this).setContentTitle("Alert")
.setContentTitle("You've received a new message")
.setSmallIcon(R.drawable.ic_cast_dark);
//Set pending intent
mNotifyBuilder.setContentIntent(resultPendingIntent);
//Set vibration
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
mNotifyBuilder.setDefaults(defaults);
// Set the content for Notification
mNotifyBuilder.setContentText("You have new notifications");
// Set autocancel
mNotifyBuilder.setAutoCancel(true);
// Post a notification
mNotificationManager.notify(notifyID, mNotifyBuilder.build());
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amosang.pushtest" >
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- GCM Permissions - Start here -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:exported="true"
android:name=".HomeActivity"
android:label="#string/title_activity_home" >
</activity>
<receiver
android:name=".GCMBroadcastReceiver"
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.example.amosang.pushtest" />
</intent-filter>
</receiver>
<service android:name=".GCMNotificationIntentService" />
<activity
android:name=".NewRequest"
android:label="#string/title_activity_new_request" >
</activity>
</application>
I can't seem to add FLAG_ACTIVITY_NEW_TASK as it doesn't compile.
Here is tested code that will help you to achieve the desired result.
private void notify(String msg) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setVibrate(new long[]{5000})
.setLights(Color.GREEN, 3000, 3000) //for notification led light with color
.setContentText(msg)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Try to use:
`PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);`
instead of
`PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);`
Please add android:exported="true" [http://developer.android.com/intl/es/guide/topics/manifest/service-element.html] in android manifest inside activity tag which is supposed to be opened after tapping notification from status bar.
I have created a Android app.I want that when it gets installed in the device it will show no GUI part. When we will broadcast the message from the server, on selection of the notification i want the user to show the GUI part. How can we do this is Android?
I tried this in my Notification function where i am starting the new Intent
setTheme(android.R.style.Theme);
This in my Manifest.XML file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.iween.gcmclient" android:versionCode="1" android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.example.iween.gcmclient.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="com.example.iween.gcmclient.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Main activity. -->
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.iween.gcmclient.DemoActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden|screenSize"
android:launchMode="singleTop">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.iween.gcmclient.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.google.android.gcm.demo.app" />
</intent-filter>
</receiver>
<service android:name="com.example.iween.gcmclient.GcmIntentService" />
</application>
</manifest>
Notification Function
private void sendNotification(String recivedMessage) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, DemoActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("Iween Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(recivedMessage))
.setContentText(recivedMessage);
mBuilder.build().flags = Notification.FLAG_AUTO_CANCEL;
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(notificationSound);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Try this way
Create one public static int variable which will update when notification receives
For eg In your receiver class
public static int THEME = 0;
onReceive(){
if(cond1){
THEME = R.style.them1;
}else if(cond2){
THEME = R.style.them2;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if(THEME!=0){
setTheme(THEME);
}
super.onCreate(savedInstanceState);