Device stops receiving notifications after 24 hours - android

I have an application in android and phonegap and I'm using Parse to send pushs.
Everything works fine when I install it: I can send and receive notifications.
After 5 hours, it starts to delay the pushs and after one day, it stops and I cant receive it anymore.
I see the notifications in dashboard but not in device.
Can someone give me a sugestion ?
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.meeto1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name="com.example.meeto1.ParseApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.meeto1.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>
<service android:name="com.parse.PushService" />
<receiver android:name="com.example.meeto1.PushReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<meta-data android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_launcher"/>
</application>
</manifest>
PushReceiver.java:
package com.example.meeto1;
import org.json.JSONException;
import org.json.JSONObject;
import com.parse.ParsePushBroadcastReceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class PushReceiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked2");
Bundle extras = intent.getExtras();
String message = extras != null ? extras.getString("com.parse.Data") : "";
JSONObject jObject;
try {
jObject = new JSONObject(message);
for (int i=0; i < 10; i++)
{
Toast toast = Toast.makeText(context, jObject.getString("alert"), Toast.LENGTH_LONG);
toast.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected Class<? extends Activity> getActivity(Context context, Intent intent) {
Log.v("Parse", "getActivity called");
return super.getActivity(context, intent);
}
#Override
protected void onPushReceive(Context context, Intent intent) {
Log.v("Parse", "onPushReceive called");
super.onPushReceive(context, intent);
}
#Override
public void onReceive(Context context, Intent intent) {
Log.v("Parse", "onReceive Called");
super.onReceive(context, intent);
}
}
MainActivity.java:
package com.example.meeto1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.cordova.DroidGap;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
this.appView.addJavascriptInterface(new MainActivity(), "Android");
this.appView.addJavascriptInterface(new MeusMetodos(), "MeusMetodos");
this.appView.getSettings().setJavaScriptEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onStop() {
Log.w(TAG, "App stopped");
super.onStop();
}
#Override
public void onDestroy() {
Log.w(TAG, "App destoryed");
super.onDestroy();
}
}
ParseApplication.java
package com.example.meeto1;
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.PushService;
import android.app.Application;
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "xxx", "xxx");
ParsePush.subscribeInBackground("todosUsuarios");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}

Just to let you know I solved it.
In my Manifest,inside <application>, I'm using the GCM Parse support and my notifications are working now:
<receiver android:name="com.parse.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" />
<category android:name="com.example.meeto1" />
</intent-filter>

Related

"Error: cannot find symbol class app"

I have external package class (my custom library), which I want to include in the project. It uses Activity, but shows error as you see.
If I place this command in main project class, it works well... What to do?
Here is the code:
package com.__MyDefaultLibrary;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.*;
import java.io.File;
import java.util.Calendar;
//
import android.app.Activity;
import com.oceanesa.samplevideorecorder.R;
public class __MyDefaultFunctions{
public android.app.Activity actv1 = android.app.Activity;
public void Initt(){
PreferenceManager.setDefaultValues(actv1.getBaseContext(), R.xml.mypreferences, false);
}
// ========== MY CUSTOM LIBRARY =============//
//button find
public Button fvb(int id) {
return (Button) actv1.findViewById(id);
}
//message show
public void msg(String text) {
Toast.makeText(actv1.getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
public View.OnClickListener optionsListener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(actv1.getBaseContext(), com.__MyDefaultLibrary.__MyDefaultPreferencesInit1.class);
actv1.startActivity(i);
}
};
}
And AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oceanesa.samplevideorecorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<activity
android:name="com.oceanesa.samplevideorecorder.VideoCaptureExample"
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.__MyDefaultLibrary.__MyDefaultPreferencesInit1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Update Answer just passing context to class
public class MyClass {
private static Context mContext;
private static Activity mActivity;
public MyClass(Context c) {
mContext= c;
}
public MyClass(Activity act) {
mActivity= act;
}
public static void showToastMethod() {
// then passing context or activity to
Toast.makeText(mContext, "mymessage ", Toast.LENGTH_SHORT).show();
// or Toast.makeText(mActivity, "mymessage ", Toast.LENGTH_SHORT).show();
}
}
Finally I got it working with the help of topic: How to display a Toast message in from a class that doesn't extend Activity
Problem was in my code. There was no need to call andorid.app at all. I didnt pass Activity to my external file, from where I couldnt call anything..
I had to pass activity like this (from main project file):
public MyExternalClass myEx = new MyExternalClass(Activity);
and needed some modifications...

Android. Intent.ACTION CAMERA BUTTON is not working

Learning broadcast messages. The book DN Kolisnichenko - "Programming for Android" is an example of where the camera at the touch of a button starts the service. But I have it che does not work. What is the problem? Or I do not know what is the camera button (physical or on the display), or do not understand what is wrong. Here is the code of the example:
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends AppCompatActivity {
MyReceiver iReceiver = new MyReceiver();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter iFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
iFilter.addAction(Intent.ACTION_CAMERA_BUTTON);
registerReceiver(iReceiver, iFilter);
}
#Override
protected void onDestroy() {
unregisterReceiver(iReceiver);
super.onDestroy();
}
}
MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context rcvContext, Intent rcvIntent) {
String action = rcvIntent.getAction();
if (action.equals(Intent.ACTION_CAMERA_BUTTON)) {
rcvContext.startService(new Intent(rcvContext,
MyService.class));
}
}
}
MyService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service started...",
Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...",
Toast.LENGTH_LONG).show();
}
}
And manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ivarious.myapplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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" />
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
<service android:name=".MyService"></service>
<action android:name="android.intent.action.CAMERA_BUTTON" />
<action android:name="android.intent.extra.KEY_EVENT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
</receiver>
</application>
</manifest>
Make Sure You Added The Camera Permission to your Manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Airpush Application information not avaliable

I have integrated Airpush Bundle SDK in my new android app following this documentation. The problem is I am not seeing any ad. I have provided the MainActivity.java, AndroidManifest.xml & LogCat below. What is the meaning of the message:
Application information not avaliable
and what do I need to do in order to fix this problem?
MainActivity.java
package mg.mynewandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.oadx.khsz210697.AdListener;
import com.oadx.khsz210697.AdListener.AdType;
import com.oadx.khsz210697.MA;
public class MainActivity extends Activity {
private ListView listView1;
private MA ma;
private AdListener adCallbackListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ma==null)
ma=new MA(this, adCallbackListener, true);
ma.callSmartWallAd();
ma.call360Ad(this, 0, false, null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
//Displaying Cached SmartWall Ad
try {
ma.showCachedAd(this, AdType.smartwall);
} catch (Exception e)
{
super.onBackPressed();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mg.mynewandroidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/ILYSTheme" >
<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>
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<meta-data android:name="com.oadx.XXXXXXXXXX.APPID" android:value="MY_APP_ID" />
<meta-data android:name="com.oadx.XXXXXXXXXX.APIKEY" android:value="android*MY_API_KEY"/>
<activity android:exported="false" android:name="com.oadx.XXXXXXXXXX.AdActivity"
android:configChanges="orientation|screenSize"
android:theme="#android:style/Theme.Translucent" />
<activity android:name="com.oadx.XXXXXXXXXX.BrowserActivity"
android:configChanges="orientation|screenSize" />
<activity android:name="com.oadx.XXXXXXXXXX.VActivity"
android:configChanges="orientation|screenSize" android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<service android:name="com.oadx.XXXXXXXXXX.LService" android:exported="false"></service>
<receiver android:name="com.oadx.XXXXXXXXXX.BootReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
LogCat
11-21 21:06:50.229: I/BunSDK(11425): Status code: 200
11-21 21:06:50.229: I/BunSDK(11425): 360 Json: {"status":204,"message":"ADS Not Available"}
11-21 21:06:50.829: I/BunSDK(11425): Status code: 200
11-21 21:06:50.829: I/BunSDK(11425): SmartWall JSON: {"status":100,"count":0,"url":"","message":"Application information not avaliable"}

IntentService is not called from BroadCastReceiver

below codes are IntentService , BoradCastReceiver , MainAcitivity, Menifest
Actually When the app is started IntentService is called from MainActivity
When called from MainActivity, it works fine.
But When called from BroadCastReceiver, it seems not called.
Please tell me what i should do to solve it.
Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.location"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.location.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>
<service
android:name=".backgraound.LocationBackGround"
android:exported="true" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyA_49-k8bdNVqMJkMTtl3hU97No3poJBzs" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<uses-library
android:name="com.google.android.maps"
android:required="true" />
<receiver
android:name=".backgraound.AlarmReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
<receiver
android:name=".backgraound.startBackGround"
android:enabled="true" >
</receiver>
</application>
</manifest>
BroadCastReceiver
package com.example.location.backgraound;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class startBackGround extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast. makeText( context , "startBackGround", Toast.LENGTH_SHORT ).show();
Intent mServiceIntent = new Intent(context, LocationBackGround.class);
context.startService(mServiceIntent);
}
}
IntentService
package com.example.location.backgraound;
import java.util.Iterator;
import java.util.List;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.example.location.db.MySQLiteHelper;
import com.example.location.meta.LocationNState;
public class LocationBackGround extends IntentService {
private MySQLiteHelper db = null;
private List<LocationNState> locationNStates = null;
private Context context = null;
private LocationManager mLocMan = null;
private Location currentLocation = null;
private AudioManager mAudioManager = null;
public LocationBackGround() {
super("LocationBackGround");
// TODO Auto-generated constructor stub
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
context = getBaseContext();
Toast.makeText( context , "intentService start : LocationBackGround", Toast.LENGTH_SHORT ).show();
db = new MySQLiteHelper(context);
locationNStates = db.getAllLocationNStates();
mLocMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
requestLocation();
}
}
When i boot up then i could see a Taost showing "StartBackGround".
But i cannot see "intentService start : LocationBackGround" Toast
is there any problem with my code?
Please teach me...
your code looks good. I use a very similar code with my service after booting the device. The difference in my project is that I call a Service - not an IntentService.
Maybe this will help you: http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183

Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) } U=0: not found

I am trying to get the registration id of a device (testing using the emulator) to set up push notifications using GCM.
I have tried the following code, which gives me the following error:
Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) } U=0: not found
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.pushnotifications"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.me.pushnotifications.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.me.pushnotifications.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.me.pushnotifications.MyBroadcastReceiver"
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"/>
<category android:name="com.me.pushnotifications"/>
</intent-filter>
</receiver>
<activity
android:name="com.me.pushnotifications.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>
</application>
</manifest>
I am using a BroadcastReceiver to achieve the purpose:
package com.me.pushnotifications;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.i("action::",action);
try{
if(action.equals("com.google.android.c2dm.intent.REGISTRATION")){
String regId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
Log.i("regId::",regId);
}
else if(action.equals("com.google.android.c2dm.intent.RECEIVE")){
String data = intent.getStringExtra("data");
Log.i("data::",data);
}
}
finally{
}
}
}
MainActivity
package com.me.pushnotifications;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context context = getApplicationContext();
final String appId = getResources().getString(R.string.appId) ;
Log.i("appName::",appId);
Button activateButton,deactivateButton;
activateButton = (Button)findViewById(R.id.activateButton);
deactivateButton = (Button)findViewById(R.id.deactivateButton);
activateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("activate::","clicked");
Intent regIntent = new Intent("com.google.android.c2dm.intent.REGISTRATION");
Log.i("intent::","created");
regIntent.putExtra("app", PendingIntent.getBroadcast(context, 0,
new Intent(), 0));
regIntent.putExtra("sender", "registration_id");
startService(regIntent);
}
});
deactivateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("deactivate::","clicked");
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
Log.i("intent::","created");
unregIntent.putExtra("app", PendingIntent.getBroadcast(context, 0,
new Intent(), 0));
startService(unregIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have installed the Google API's for API Level 18. What may be the reason for the issue?
add this line in your intent-filter tag in manifest
file
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
Like this
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.androidhive.pushnotifications" />
</intent-filter>
Use like this:
<receiver
android:name="com.example.gcmclient.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTER" />
<category android:name="com.example.gcmclient" />
</intent-filter>
</receiver>
<service android:name="com.example.gcmclient.GcmMessageHandler" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<service android:name=".GcmIntentService" />

Categories

Resources