Not a single guide working for Push notifications - android

I tried every guide there + youtube but nothing works
even when using a custom listener like ( not all the manifest included , too big) :
public class MyReceiver extends ParsePushBroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//code here
} }
Manifest
<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.VIBRATE" />
<receiver
android:name=".MyReceiver"
android:enabled="true"
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>

Have you seen official Google developer blog
Clone sample app from here replace you app id and test, it will work.
Hope this will help !!!

Related

BroadcastReceiver doesn't receive the broadcast event message

I created my broadcast receiver AnprEventReciever that should be triggered when connectivity state changes, however it doesn't.
AnprEventReciever:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import bi.anpr.layouts.SplashActivity;
public class AnprEventReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("test","event recieved");
Toast.makeText(context, "event recieved" , Toast.LENGTH_LONG).show();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxx.vlctest">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="bi.anpr.vlc.VLCApplication"
android:allowBackup="false"
android:icon="#mipmap/ic_anpr_launcher_round"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name="bi.anpr.core.AnprEventReciever"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="bi.anpr.layouts.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.VideoResourceActivity"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.ZoneActivity"
android:label="Zone Setter"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SettingsActivity"
android:label="#string/title_activity_settings"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SplashActivity"
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>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
Please note that i'd tested almost every possible action that all failed except the BOOT_COMPLETED, where my BroadcastReceiver was successfully fired.
Thanks for #TheWanderer who helped me through his comment to find the answer. In the Android 8.0 Behavior Changes Note developers mentioned the following:
Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).
So i tried to programatically registering my BroadcastReceiver like this below instead of creating a manifest entry for it:
private AnprEventReciever myReceiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_resource);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
myReceiver = new AnprEventReciever();
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
It works perfectly, however unfortunately the action "android.intent.action.CONNECTIVITY_CHANGE" seems to be deprecated where i couldn't find it in the Intent class and i don't know if it is found elsewhere.
Note: as i go through reading i found that it is necessary to un-register the broadcast receiver when the activity pauses or destroys, otherwise you might get an error when trying to register or re-register it.
UPDATE:
Create intent filter with ConnectivityManager.CONNECTIVITY_ACTION instead of "android.intent.action.CONNECTIVITY_CHANGE" in order to receive connectivity changed broadcasts.

Android receive GCM while display is off

I am using GCM to send some data from my server to my app.
The messaging is working fine. I receive every massage instantly on my device.
However, The problem is: It does not work when the display of my smartphone is off.
Maybe it has s.th. to do with my smartphone? I'm running Android 5 and notification from other apps work fine (like whatsapp).
server data:
{
"data": {
"id": "5",
"message": "test"
},
"registration_ids": ["myRegId"],
"priority": "high"
}
Manifest:
...
<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>
<service
android:name=".GcmMessageHandler"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
<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="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.asdf.myapp.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.asdf.myapp.gcm.permission.C2D_MESSAGE" />
Receiver:
public class GcmMessageHandler extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
Log.d("test", "message received");
}
}
Your receiver class should extend from WakefulBroadcastReceiver to guaranteeing that the CPU is awake so that your listener service can complete its task even if your device is lock.
Manifest
<permission
android:name="com.oostaa.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.oostaa.app.permission.C2D_MESSAGE" />
<receiver
android:name=".receiver.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.oostaa.app" />
</intent-filter>
</receiver>
Note: com.oostaa.app is my package name.
Receiver:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
I noticed, that the issue only occurs if I'm connected to my WLAN.
When WLAN is disabled, I get the GCM message no matter what.
I've read this reddit post and followed its instructions which seemed to solve my issues.
Reddit

Android parse notifications without app being in background?

I couldn't find a page or question where it can tell me how to make a notification without the app running in background or it being opened.
It would be great if anyone could help. (I'm using Android)
First step is to create a Parse account and get your Application Id and Client Key.
After that you have to create your own custom Application class by creating a class that extends Application and then override onCreate (just like you would any activity) and place that line in.
public class MyApplication extends Application {
public void onCreate() {
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
You also have to tell the manifest that you are using a custom application class. You can do this by, in your AndroidManifest.xml file, you will have to set the name element to the location of you new Application class:
<application
android:name="com.packageName.example.MyApplication"
android:label="#string/app_name"
android:logo="#drawable/ic_launcher_no_text" >
In the Manifest file
Declare the following permissions:
<permission android:name="com.packagename.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.packagename.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<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="android.permission.INTERNET"/>
Also declare a service and receiver in the manifest:
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParsePushBroadcastReceiver" 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.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.packagename.android" />
</intent-filter>
</receiver>
Change com.packagename to your package name too!
Finally go to your parse account and try sending a push notification from there.

new Google Cloud Messaging API

I'm sending a push notification using PHP using curl, and the result seems to return fine:
{"multicast_id":2345735671345346,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:13456457587969%375ed23445237ecd"},{"message_id":"0:12344526107806%375ed3439fd7ecd"}]}
So I'm guessing the message is sent, and the problem is on the BroadcastReceiver subclass.
My Broadcast receiver code:
package com.example.myexample.pushnotifications;
public class GCMBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
Log.d(TAG, "onReceive method executed properly");
//NotificationManager and NotificationCompat.Builder code to build a notification
}
}
then my AndroidManifest.xml for the permissions:
<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" />
<permission
android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE" />
and for the receiver inside the application tag:
<receiver
android:name="com.example.myexample.pushnotifications.GCMBroadcastReceiver"
android:enabled="true"
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.myexample.pushnotifications" />
</intent-filter>
</receiver>
The problem is that the broadcast receiver never receives the message.
Assuming the package of the app is com.example.myexample, the following changes are required :
<permission
android:name="com.example.myexample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.permission.C2D_MESSAGE" />
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.myexample" />
</intent-filter>

My Broadcast Receiver does not start

I have some problem with BroadcastReceiver. There nothing happens when I catch an outgoing call.
public class demoBroadcastReceiver extends BroadcastReceiver {
/** Called when the activity is first created. */
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "there is new calling", Toast.LENGTH_LONG).show();
}
}
This is content of my Manifest:
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".demoBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Updated:
Thanks Lucifer for solution:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
I also need this:
<uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />**strong text**
You should declare this permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
maybe you should post your log here, so we can analyse it.
try add this permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Hope this
put this in your manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and this in your application node..
<receiver android:name="com.example.demoBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <--- add this intent filter
</intent-filter>
</receiver>
this is to start a service when a device boots up on android

Categories

Resources