I do receive the push messages but whenever I click on them inside the notificationbar this logcat appears and the notification dissapears...
This is my logcat output:
05-31 16:49:47.165: D/Test - UALib(20523): No intent receiver set, not sending ACTION_NOTIFICATION_OPENED
Here is my manifest structure:
<category android:name="com.test.push" />
</intent-filter>
</receiver>
<service
android:name="com.urbanairship.push.PushService"
android:label="Push Notification Service" />
<service
android:name="com.urbanairship.push.PushWorkerService"
android:label="Push Notification Worker Service" />
<service
android:name="com.urbanairship.analytics.EventService"
android:label="Event Service" />
<provider
android:name="com.urbanairship.UrbanAirshipProvider"
android:authorities="com.test.push.urbanairship.provider"
android:exported="false"
android:multiprocess="true" />
<activity
android:name="com.test.push.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:name="com.test.push.SettingsActivity"
android:label="#string/title_activity_settings" />
<activity
android:name="com.test.push.ChooseClass"
android:label="#string/app_name" />
</application>
This here is my MyApplication.java:
public class MyApplication extends Application {
#Override
public void onCreate() {
AirshipConfigOptions options = AirshipConfigOptions
.loadDefaultOptions(this);
options.developmentAppKey = "1234567890";
options.developmentAppSecret = "1234567890";
options.productionAppKey = "";
options.productionAppSecret = "";
options.gcmSender = "1234567890";
options.transport = "gcm";
options.inProduction = false;
UAirship.takeOff(this, options);
PushManager.enablePush();
}
}
And this is my IntentReceiver.java:
public class IntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context aContext, Intent aIntent) {
String action = aIntent.getAction();
if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
logPushExtras(aIntent);
} else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
logPushExtras(aIntent);
Intent launch = new Intent(Intent.ACTION_MAIN);
launch.setClass(UAirship.shared().getApplicationContext(),
MainActivity.class);
launch.putExtra("push_message",
aIntent.getStringExtra(PushManager.EXTRA_ALERT));
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UAirship.shared().getApplicationContext().startActivity(launch);
} else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) {
Logger.info("device registrated, APID="
+ aIntent.getStringExtra(PushManager.EXTRA_APID)
+ ", valid="
+ aIntent.getBooleanExtra(
PushManager.EXTRA_REGISTRATION_VALID, false));
}
}
private void logPushExtras(Intent intent) {
Set<String> keys = intent.getExtras().keySet();
for (String key : keys) {
List<String> ignoredKeys = (List<String>) Arrays.asList(
"collapse_key", "from", PushManager.EXTRA_NOTIFICATION_ID,
PushManager.EXTRA_PUSH_ID, PushManager.EXTRA_ALERT);
if (ignoredKeys.contains(key)) {
continue;
}
}
}
}
The problem was, I was missing the following line inside my MyApplication.java file:
PushManager.shared().setIntentReceiver(IntentReceiver.class);
All the rest was correct. It's now working perfectly.
And i was missing to specify Application's android:name attribute in my manifest file-
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:allowClearUserData="true"
android:enabled="true"
android:name=".MyMainApplication">
<activity>
Some Activity
</activity>
</application>
Related
how to launch an launch activity after phone restart or manually turn power off or on
i tried below codes but its working for only for unlocking phone but i also want to make it done for boot up manually or restart phone.
below is code
application.class
public class MyApplication extends Application {
public static SharedPreferences preferences;
public static SharedPreferences.Editor editor;
public static String PASSWORD = "password";
public static String IS_SET = "nothing";
public static String MYPREF = "mypref";
public String TAG = getClass().getName();
#Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(this);
}
#Override
public void onCreate() {
super.onCreate();
init();
}
public void init() {
preferences = getSharedPreferences(MYPREF, 0);
editor = preferences.edit();
startService(new Intent(getBaseContext(), ScreenReceiver.class));
}
}
ScreenReceiver.class
public class ScreenReceiver extends BroadcastReceiver {
SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getAction());
preferences = context.getSharedPreferences(MYPREF, 0);
if (preferences != null) {
String lock = preferences.getString(IS_SET, null);
if (lock != null) {
if (lock.equals("passcode")) {
gotopasslock(context, intent);
} else {
gotopatternlock(context, intent);
}
}
}
}
public void gotopasslock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, Main.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, Main.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
public void gotopatternlock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, PatternLock.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, PatternLock.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
}
Main.class
public class Main extends AppCompatActivity {
private PinLockView mPinLockView;
private IndicatorDots mIndicatorDots;
private String TAG = getClass().getName();
String oldPassword = "";
String newPassword = "";
SharedPreferences preferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.main_xml);
menifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="label">
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
<activity android:name=".activities.ChooseActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.Home" />
<activity android:name=".activities.MainActivity" />
<activity
android:name=".activities.Main"
android:showOnLockScreen="true" />
<activity
android:name=".activities.PatternLock"
android:showOnLockScreen="true" />
<!--<receiver
android:name=".receiver.ScreenReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
</receiver>-->
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Intent Action is missing.
To make a android activity launch after reboot, add following line in
AndroidManifest.xml
STEP 1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2:
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"
</intent-filter>
</receiver>
NOTE:
try to do code cleanup
I' m trying to implement push notification in my applicatio but the RegistrationIntentService.class does not get called when i use the method startService.
home activity:
public class HomeActivity extends AriesMobileActivity {
private LinearLayout itemReports;
private LinearLayout itemTickets;
private LinearLayout itemSystems;
private LinearLayout itemManuals;
private LinearLayout itemNotes;
private LinearLayout itemChecklist;
private LinearLayout itemCalendar;
private LinearLayout itemSettings;
private BroadcastReceiver mRegistrationBroadcastReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences
.getBoolean(Preferences.SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
} else {
}
}
};
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(this, resultCode, 9000)
.show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
}
Intent intent = new Intent(this,RegistrationIntentService.class);
startService(intent);
if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
this.setContentView(R.layout.layout_home_landscape);
else this.setContentView(R.layout.layout_home_portrait);
itemReports = (LinearLayout) this.findViewById(R.id.item_menu_reports);
itemTickets = (LinearLayout) this.findViewById(R.id.item_menu_tickets);
itemSystems = (LinearLayout) this.findViewById(R.id.item_menu_systems);
itemManuals = (LinearLayout) this.findViewById(R.id.item_menu_manuals);
itemNotes = (LinearLayout) this.findViewById(R.id.item_menu_notes);
itemChecklist = (LinearLayout) this.findViewById(R.id.item_menu_checklist);
itemCalendar = (LinearLayout) this.findViewById(R.id.item_menu_calendar);
itemSettings = (LinearLayout) this.findViewById(R.id.item_menu_settings);
itemReports.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ReportListActivity.class);
startActivity(intent);
}
});
itemTickets.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, TicketListActivity.class);
startActivity(intent);
}
});
itemSystems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SystemListActivity.class);
startActivity(intent);
}
});
itemSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
context.startActivity(new Intent(context, SettingsActivity.class));
}
});
itemManuals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ManualsListActivity.class);
startActivity(intent);
}
});
itemCalendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, CalendarActivity.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Preferences.REGISTRATION_COMPLETE));
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
}
RegistrationIntentService:
public class RegistrationIntentService extends IntentService {
private static final String[] TOPICS = {"global"};
public RegistrationIntentService() {
super("RegistrationIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// [START register_for_gcm]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
// R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
// See https://developers.google.com/cloud-messaging/android/start for details on this file.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken("312109676786",
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// [END get_token]
Log.i("RegistrationIntentService", "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
// Subscribe to topic channels
subscribeTopics(token);
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(Preferences.SENT_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
} catch (Exception e) {
Log.d("RegistrationIntentService", "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(Preferences.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(Preferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
/**
* Persist registration to third-party servers.
*
* Modify this method to associate the user's GCM registration token with any server-side account
* maintained by your application.
*
* #param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
/**
* Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
*
* #param token GCM token
* #throws IOException if unable to reach the GCM PubSub service
*/
// [START subscribe_topics]
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null);
}
}
// [END subscribe_topics]
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ngs.ariesmobile"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.verivo.examples.pushandalerttests.permission.C2D_MESSAGE" />
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<application
android:name=".AriesMobileApplication"
android:allowBackup="true"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light.NoActionBar" >
<service
android:name=".AriesMobileGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".AriesMobileInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service android:name=".RegistrationIntentService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<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="com.example.gcm" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<receiver
android:name="com.verivo.examples.pushandalerttests.MyBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.verivo.examples.pushandalerttests" />
</intent-filter>
</receiver>
<receiver android:name=".receivers.PhoneCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".view.LoginActivity"
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:name=".view.HomeActivity"
android:label="#string/app_name" />
<activity
android:name=".view.SignatureCaptureActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".view.SetupHomeActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SetupApiActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SetupLoginActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SetupTechnicianActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SettingsActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SettingsPasswordActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SettingsApiActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.SynchronizationActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.ReportCustomerActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".view.ReportInternalActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".view.ReportListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.TicketListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.PhoneCallActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent" />
<activity
android:name=".view.SystemActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name"
android:screenOrientation="landscape" />
<activity
android:name=".view.SystemListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name" />
<activity
android:name=".view.ReportSummaryActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait" />
<activity
android:name=".view.ManualsListActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/title_activity_manuals_list" >
</activity>
<activity
android:name=".view.SystemStoricalActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/title_activity_system_storical" >
</activity>
<activity
android:name=".view.SystemTechnicalPartActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/title_activity_system_technical_part" >
</activity>
<activity
android:name=".view.SystemConsActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/title_activity_system_cons" >
</activity>
<activity
android:name=".view.SystemNoteActivity"
android:configChanges="orientation|keyboard|screenSize"
android:label="#string/title_activity_system_note" >
</activity>
<activity
android:name=".view.SystemSupervisionActivity"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_system_supervision" >
</activity>
<activity
android:name=".view.CalendarActivity"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_calendar"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".view.ReportHoursActivity"
android:label="#string/title_activity_report_hours" >
</activity>
<activity
android:name="ngs.ariesmobile.pushnotification.RegisterActivity"
android:label="#string/title_activity_register" >
</activity>
</application>
</manifest>
Thanks in advance
I was debugging my app and I have experienced an interesting issue. I had a breakpoint in my onReceive method and the program has stopped there only when screen of my phone was turned on (awake).
Why is that?
If by any chance you would need my code:
private void gcmInit() {
mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences
.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
// mInformationTextView.setText(getString(R.string.gcm_send_message));
} else {
// mInformationTextView.setText(getString(R.string.token_error_message));
}
}
};
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
Plus I have also these 2 methods in my MainActivity:
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
My Manifest, where I register the receiver:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<!-- GCM START -->
<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="com.example.android.bluetoothchat" />
</intent-filter>
</receiver>
<service
android:name="com.example.android.gcm.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.android.gcm.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="com.example.android.gcm.RegistrationIntentService"
android:exported="false">
</service>
<!-- GCM END -->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
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:name=".DeviceListActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/select_device"
android:theme="#android:style/Theme.Holo.Dialog"/>
</application>
I have the AlarmMainActivity which broadcasts an intent at a time set by the alarm. And the receiver program AlarmReceiver should catch this intent and send a notification. From the log, I can see that the alarm is being set, but the receiver doesn't start and its not working. Can you look at my code below and please let me know why the BroadcastReceiver is not working. Thanks.
AlarmMainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_main);
Intent alertIntent = new Intent(this, AlarmReceiver.class);
final PendingIntent pendingIntent =
PendingIntent.getBroadcast
(this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT);
final Button alarmButton = (Button)findViewById(R.id.alarm_button);
alarmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Long alertTime = new
GregorianCalendar().getTimeInMillis()+5*1000;
Log.i(TAG,"Alarm will be sent at : "+ alertTime.toString());
AlarmManager am =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,alertTime,pendingIntent );
Log.i(TAG, "Alarm is now set");
}
});
}//oncreate
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver{
private static final String TAG = "AlarmReceiver";
private String msgTitle, msgText, msgTicker;
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Entered AlarmReceiver()");
msgTitle = "Todays Weather";
msgText = "Its Sunny and Warm";
msgTicker="Alert";
PendingIntent notifyIntent = PendingIntent.getActivity(context,0, new
Intent(context, AlarmMainActivity.class),0);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(context).
setSmallIcon(R.drawable.weather_image).
setTicker(msgTicker).
setContentTitle(msgTitle).
setContentText(msgText).
setDefaults(NotificationCompat.DEFAULT_SOUND).
setAutoCancel(true).
setContentIntent(notifyIntent);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService
(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,mBuilder.build());
Log.i(TAG,"Notification Sent");
}
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thirdlaw.alertalarm" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true">
<activity
android:name=".AlarmMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
<intent-filter>
<action android:name="MY_ACTION_STRING"/>
</intent-filter>
</receiver>
</activity>
</application>
</manifest>
Remove the <receiver /> tag out of the <activity /> tag like :
<activity
android:name=".AlarmMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
<intent-filter>
<action android:name="MY_ACTION_STRING"/>
</intent-filter>
</receiver>
I am trying to start a foreground service at boot time, but it never starts, however If i try to start a normal background service. It starts perfectly fine.
Can you please let me what is wrong with my code ?
My code is:
Manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="com.test.andsrvfg.AndSrvFgService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="AndSrvFgService">
<intent-filter>
<action android:name="com.test.andsrvfg.AndSrvFgService"></action>
</intent-filter>
</service>
</application>
BroadcastReceiver to handle ACTION_BOOT_COMPLETED:
public class AndSrvFgStarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.test.andsrvfg.AndSrvFgService");
context.startService(i);
}
}
}
The actual service is like:
public class AndSrvFgService extends Service {
private boolean bForeground = false;
public AndSrvFgService() {
}
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!bForeground) {
bForeground = true;
Notification note = new Notification( 0, null, System.currentTimeMillis() );
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground( 1242, note );
}
return START_STICKY;
}
#Override
public void onDestroy() {
if(bForeground) {
stopForeground(true);
}
}
This code works for me:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dfsdf"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<service android:name=".MyService"/>
<receiver android:enabled="true"
android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
Put this in the receiver:
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
Set enable to true:
android:enabled="true"