Starting Activity from other Class - android

Google map is displayed here and I also want to issue notification and when I click it, notification should take me to the main activity ie, google map but notification is not issued . Here are 2 classes and manifest file.
public class MainActivity extends FragmentActivity{
protected GoogleMap gMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (gMap == null) {
Toast.makeText(this, "Google Maps not available",
Toast.LENGTH_LONG).show();
gMap.setMyLocationEnabled(true);
}
}
Notify Class,
public class Notify extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.jollyr)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle("Flood Warning")
.setContentText("You are in Danger Zone");
// Sets an ID for the notification
int notId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(notId, mBuilder.build());
Intent i=new Intent(this,MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
i,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
}
}
Manifest file,
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.map.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.example.map.Notify"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.map.Notify" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="Package+.v2.API_KEY"
android:value="Key here" />
</application>
</manifest>

Related

Unable to show notification when app is closed

I am trying to show notification at specific date I am using alarm manager and broadcast receiver to show notifications but the problem is notification works only when app is open and when app is closed notification does not show. Below is my code:
Reminder.java
public class Reminder extends AppCompatActivity {
long reminderDateTimeInMilliseconds = 000;
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
but = findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotifyChannel();
Intent intent = new Intent(Reminder.this,ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this,0,intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();
//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2020, 8, 20, 19, 12, 0);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
alarmManager.setExact(AlarmManager.RTC_WAKEUP,reminderDateTimeInMilliseconds,pendingIntent);
}
});
}
private void createNotifyChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "ReminChannel";
String desc = "This is my channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("mynotif",name,importance);
channel.setDescription(desc);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
ReminderBroadcast.java
public class ReminderBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder notif = new NotificationCompat.Builder(context,"mynotif")
.setContentTitle("Appointment reminder")
.setContentText("Hello there")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(200,notif.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.firstapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Reminder">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ThirdActivity" />
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
</activity>
<receiver android:name=".ReminderBroadcast"/>
</application>
</manifest>
What am I doing wrong?

Notification click is not working

i m generating notification.so i created the notification and i m getting the notification when i click the button.now,i created onclick function and some toast,but when i clicked on thone button in notificatio i m getting toast message. i dont know whats wrong in below code.so please help.thanks in adavance.
this is my broadcast activity
public class NotificationBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(NotificationGenerator.NOTIFY_PLAY))
{
Toast.makeText(context,"NOTIFY PLAY",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_PAUSE))
{
Toast.makeText(context,"NOTIFY PAUSE",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_NEXT))
{
Toast.makeText(context,"NOTIFY NEXT",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(NotificationGenerator.NOTIFY_DELETE))
{
Toast.makeText(context,"NOTIFY DELETE",Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals("com.example.murarilal.atry.previous"))
{
Toast.makeText(context,"NOTIFY PREVIOUS",Toast.LENGTH_LONG).show();
}
}
}
this is notifiction builder activity
public class NotificationGenerator{
public static final String NOTIFY_PREVIOUS="com.example.murarilal.atry.previous";
public static final String NOTIFY_DELETE="com.example.murarilal.atry.delete";
public static final String NOTIFY_PAUSE="com.example.murarilal.atry.pause";
public static final String NOTIFY_PLAY="com.example.murarilal.atry.play";
public static final String NOTIFY_NEXT="com.example.murarilal.atry.next";
private static final int NOTIFICATION_ID_OPEN_ACTIVITY=1;
private static String ID = "default";
public static void openActivityNotification(Context context) {
RemoteViews expandView=new RemoteViews(context.getPackageName(),R.layout.notification);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
int requestID = (int) System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setChannelId(ID);
Intent notifyIntent=new Intent(context,BlankFragment2.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,requestID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
Intent intent = new Intent(context, BlankFragment2.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ed)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle("try")
.setCustomBigContentView(expandView)
.setContentText("notification")
.setContentInfo("Info");
notificationManager.notify(NOTIFICATION_ID_OPEN_ACTIVITY, notificationBuilder.build());
}
public static void customBigNotification(Context context)
{
int requestID = (int) System.currentTimeMillis();
RemoteViews expandView=new RemoteViews(context.getPackageName(),R.layout.notification);
NotificationCompat.Builder nc=new NotificationCompat.Builder(context);
NotificationManager nm=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notifyIntent=new Intent(context,BlankFragment2.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,requestID,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_play_circle_filled_black_24dp);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandView);
nc.setContentTitle("music player");
nc.setContentText("control audio");
nc.getBigContentView().setTextViewText(R.id.songName,"shape of you");
setListeners(expandView,context);
}
private static void setListeners(RemoteViews views, Context context)
{
int requestID = (int) System.currentTimeMillis();
Intent previous= new Intent(NOTIFY_PREVIOUS);
Intent next=new Intent(NOTIFY_NEXT);
Intent delete=new Intent(NOTIFY_DELETE);
Intent play=new Intent(NOTIFY_PLAY);
Intent pause=new Intent(NOTIFY_PAUSE);
PendingIntent pPrevious=PendingIntent.getBroadcast(context,requestID,previous,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.previous,pPrevious);
PendingIntent pDelete=PendingIntent.getBroadcast(context,requestID,delete,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.cancel,pDelete);
PendingIntent pNext=PendingIntent.getBroadcast(context,requestID,next,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.next,pNext);
PendingIntent pPlay=PendingIntent.getBroadcast(context,requestID,play,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.play,pPlay);
PendingIntent pPause=PendingIntent.getBroadcast(context,requestID,pause,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.play,pPause);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.murarilal.atry">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/mytheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
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=".screen"
android:label="#string/title_activity_screen"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".com.layout.blankFragment2"
android:label="#string/hello_blank_fragment"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.albumSongs"
android:label="#string/title_activity_album_songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name="com.exlayout.Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".albumSongs"
android:label="#string/title_activity_album_songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".albumSong"
android:label="#string/title_activity_album_song"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".BlankFragment2"
android:label="#string/hello_blank_fragment"
android:theme="#style/AppTheme.NoActionBar" />
<receiver android:name=".NotificationBroadcast">
<intent-filter>
<action android:name="com.example.murarilal.atry.previous" />
<action android:name="com.example.murarilal.atry.delete" />
<action android:name="com.example.murarilal.atry.pause" />
<action android:name="com.example.murarilal.atry.next" />
<action android:name="com.example.murarilal.atry.play" />
</intent-filter>
</receiver>
<activity
android:name=".genreSongs"
android:label="#string/title_activity_genre_songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.genreSongs"
android:label="#string/title_activity_genre_songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".artist_Songs"
android:label="#string/title_activity_artist__songs"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name="com.exlayout.artist_Songs"
android:label="#string/title_activity_artist__songs"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" />
<activity
android:name=".checkActivity"
android:label="#string/title_activity_check"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".favoutitesActivity"
android:label="#string/title_activity_favoutites"
android:theme="#style/AppTheme" />
<activity
android:name=".recent"
android:label="#string/title_activity_recent"></activity>
</application>
</manifest>

Launcher Activity changed that's why FCM Push Notification doesn't work while app in background

I've changed my Launcher Activity instead of .MainActivity. The Firebase push notifications service doesn't work when my app is in the background but it works fine when the app is in the foreground.
I've added a WelcomeSlider in my app that's why I have to keep Welcome slider as the Launcher Activity. I checked that if I change the launcher activity to NotifyActivity then it works fine again.
here is my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".WelcomeActivity"
android:label="#string/app_name"
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=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Vlog"
android:parentActivityName=".MainActivity" />
<activity android:name=".NotifyActivity"></activity>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Here is myFirebaseInstanceIdService:
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh(){
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN",token);
}
}
Here is myFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this,NotifyActivity.class);
if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Bundle bundle = new Bundle();
bundle.putString("url",url);
intent.putExtras(bundle);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.notifyicon);
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(sound);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
}
}
I've solved this problem by my own.I just keep the MainActivity as a launcher Activity from Android Manifest like
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and post below code inside MainActivity onCreate for checking if its the first time launch or not!
//Check if it first time launching..
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}
it worked for me! :) :)

Unable to receive push notifications (android)

I have written very simple code (just for demo) for push notification following google developers guide. I am sending notification using Postman but I can't receive it on my emulator. I am using blue stacks with google play services. Here is my code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
String token = null;
InstanceID instanceID = InstanceID.getInstance(MainActivity.this);
try {
token = instanceID.getToken("590106578883",
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} catch (IOException e) {
Log.d("TAG", e.getMessage());
}
Log.d("TAG", token + "");
int i = 0;
}
}).start();
} // onCreate
} // MainActivity
GcmListener.java
public class MyGcmListenerService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
sendNotification(data.toString());
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
} // MyGcmListenerService
Manifest.java
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.topnotchdev.pushnotifications.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<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>
<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="gcm.play.android.samples.com.gcmquickstart" />
</intent-filter>
</receiver>
<service
android:name=".MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
Here is the response from google server
{
"multicast_id": 7574264493447786438,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1459889454670774%d32a6865f9fd7ecd"
}
]
}

Custom Notification doesn't run on Android Wear

I'm playing with Wear SDK and trying to create a wear application.
I want to display a custom notification as shown on android docs but it doesn't work.
This is my Activity Code:
public class WearActivity extends Activity {
private Button notifyBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
notifyBtn = (Button) stub.findViewById(R.id.notifyBtn);
notifyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotification();
}
});
}
});
}
private void createNotification(){
Toast.makeText(this, "Press", Toast.LENGTH_LONG).show();
//Create Intent
Intent notificationIntent =
new Intent(this, WearNotificationActivity.class)
.putExtra("EXTRA_STRING", "Hi, I'm an EXTRA!");
PendingIntent pendingNotificationIntent =
PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification =
new Notification.Builder(this)
.extend(new Notification.WearableExtender()
.setDisplayIntent(pendingNotificationIntent)
.setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
.build();
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
Although the button press is triggered, no Custom notification appears.
I edited the Manifest Too:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tizianobasile.wearactivity" >
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.DeviceDefault.Light" >
<activity
android:name=".WearActivity"
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=".WearNotificationActivity"
android:label="#string/title_activity_wear_notification"
android:exported="true"
android:allowEmbedded="true"
android:taskAffinity=""
android:theme="#android:style/Theme.DeviceDefault.Light">
</activity>
</application>
</manifest>
I really can't find the cause, my code is almost identical to the documentation code.
In general, the smallIcon is mandatory for a notification in Android Wear. From my tests, despite that in a notification with custom card layout (setDisplayIntent) the icon is not even displayed - you still need to specify it in order to appear on Android Wear at all.
For example:
Notification notification =
new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.extend(new Notification.WearableExtender()
.setDisplayIntent(pendingNotificationIntent)
.setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
.build();

Categories

Resources